-
Notifications
You must be signed in to change notification settings - Fork 707
Expand file tree
/
Copy pathapp.ts
More file actions
40 lines (33 loc) · 1.2 KB
/
app.ts
File metadata and controls
40 lines (33 loc) · 1.2 KB
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
import express, { Request, Response, NextFunction } from 'express';
import Logger from './core/Logger';
import bodyParser from 'body-parser';
import cors from 'cors';
import { corsUrl, environment } from './config';
import './database'; // initialize database
import { NotFoundError, ApiError, InternalError } from './core/ApiError';
import routesV1 from './routes/v1';
process.on('uncaughtException', (e) => {
Logger.error(e);
});
const app = express();
app.use(bodyParser.json({ limit: '10mb' }));
app.use(bodyParser.urlencoded({ limit: '10mb', extended: true, parameterLimit: 50000 }));
app.use(cors({ origin: corsUrl, optionsSuccessStatus: 200 }));
// Routes
app.use('/v1', routesV1);
// catch 404 and forward to error handler
app.use((req, res, next) => next(new NotFoundError()));
// Middleware Error Handler
// eslint-disable-next-line @typescript-eslint/no-unused-vars
app.use((err: Error, req: Request, res: Response, next: NextFunction) => {
if (err instanceof ApiError) {
ApiError.handle(err, res);
} else {
if (environment === 'development') {
Logger.error(err);
return res.status(500).send(err.message);
}
ApiError.handle(new InternalError(), res);
}
});
export default app;