-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.ts
36 lines (29 loc) · 948 Bytes
/
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
import express, { NextFunction, Request, Response } from "express";
import createError from "http-errors";
import AppMiddlewares from "./middlewares";
import indexRouter from "./routes/index";
// Initialise express
const app = express();
// Add Middlewares
AppMiddlewares(app);
// Add routes
app.use("/api/v1", indexRouter);
// 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.boomify(err, {
statusCode,
message: err.message,
});
});
export default app;