forked from Real-Dev-Squad/calendar-API
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.ts
41 lines (34 loc) · 1.04 KB
/
app.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import express, { NextFunction, Request, Response } from "express";
import createError from "http-errors";
import AppMiddlewares from "./middlewares";
import indexRouter from "./routes/index";
import health from "./controllers/health";
import Boom from "@hapi/boom";
// Initialise express
const app = express();
// Add Middlewares
AppMiddlewares(app);
// Add routes
app.use("/api/v1", indexRouter);
app.get("/", health);
// catch 404 and forward to error handler
app.use(function (req: Request, _res: Response, next: NextFunction) {
logger.error(`API not found:: ${req.originalUrl}`);
next(createError(404));
});
// error handler
app.use(function (err: any, req: Request, res: Response, _next: NextFunction) {
logger.error(
`Error handling the request:: ${req.originalUrl}, error:: `,
err
);
// eslint-disable-next-line @typescript-eslint/strict-boolean-expressions
const statusCode: number = err.status || 500;
return res.boom(
Boom.boomify(err, {
statusCode,
message: err.message,
})
);
});
export default app;