-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.js
72 lines (56 loc) · 1.95 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
61
62
63
64
65
66
67
68
69
70
71
72
const fs = require('fs');
const express = require('express');
const cookieParser = require('cookie-parser');
const requestLogger = require('morgan');
const compression = require('compression');
const swaggerUi = require('swagger-ui-express');
const config = require('config');
const indexRouter = require('./routes/index');
const {
notFound,
errorHandler,
prismaNotFoundHandler,
assertionErrorHandler,
axiosErrorHandler,
prismaConstraintFailedHandler,
} = require('./middleware/error');
// Register application
const app = express();
// remove fingerprinting header
app.disable('x-powered-by');
// request logger - https://github.com/expressjs/morgan
app.use(requestLogger('dev'));
// request parsing middleware
app.use(express.json({ limit: '50mb' }));
// extended: false -> use querystring instead of qs library to parse urlencoded query string
// removes ? ex: ?a=b will be {a: b}
// does not parse nested objects:
// ?person[name]=bobby&person[age]=3 will be { 'person[age]': '3', 'person[name]': 'bobby' }
// see https://stackoverflow.com/questions/29960764/what-does-extended-mean-in-express-4-0
app.use(express.urlencoded({ limit: '50mb', extended: false }));
app.use(cookieParser());
// compress all responses
app.use(compression());
if (!['production', 'test'].includes(config.get('mode'))) {
// mount swagger ui
try {
const swaggerFile = JSON.parse(fs.readFileSync('./swagger_output.json'));
app.use('/doc', swaggerUi.serve, swaggerUi.setup(swaggerFile));
} catch (e) {
console.error('Unable to load "./swagger_output.json"', e);
}
}
// mount router
app.use('/', indexRouter);
// handle unknown routes
app.use(notFound);
// handle prisma errors
app.use(prismaNotFoundHandler);
app.use(prismaConstraintFailedHandler);
// handle asserions errors and send 400
app.use(assertionErrorHandler);
// handle axios errors
app.use(axiosErrorHandler);
// pass any unhandled errors to the error handler
app.use(errorHandler);
module.exports = app;