-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathapp.js
60 lines (47 loc) · 1.59 KB
/
app.js
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
// # Special function to handle throwing error globally within every route and middleware
import '#lib/routerExceptionHandler';
// # Package imports
import cors from 'cors';
import helmet from 'helmet';
import morgan from 'morgan';
import express from 'express';
import cookieParser from 'cookie-parser';
// # Other imports
import log from '#logger';
import config from '#config';
import routes from '#routes';
import { idLogsMiddleware, rateLimiterMiddleware } from '#middlewares';
import { normalErrorHandler, standardErrorHandler, finalErrorHandler } from '#lib/responseHandlers';
// # Create express application
let app = express();
// # Check proxy enabled or not
if (config.proxy) app.set('trust proxy', config.proxy);
// # Remove x-powered-by header
app.disable('x-powered-by');
const corsOptions = {
credentials: true,
origin: function (origin, callback) {
if (config.isDev || !origin || ~config.corsWhitelist.indexOf(origin)) callback(null, true);
else callback(new Error('Not allowed by CORS'), null);
},
};
const morganConfig = {
stream: { write: (msg) => log.info(msg.trim()) },
};
app.use('/', express.static('public'));
// # Setup middlewares
app.use(idLogsMiddleware);
app.use(morgan(config.isDev ? 'dev' : 'combined', morganConfig));
app.use(helmet());
app.use(cors(corsOptions));
app.use(cookieParser());
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(rateLimiterMiddleware);
// # Initialize routes
app.use(routes);
// # Error handlers
app.use(normalErrorHandler);
app.use(standardErrorHandler);
app.use(finalErrorHandler);
export default app;