-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
86 lines (71 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
const mongoSanitize = require('express-mongo-sanitize');
const comprassion = require('compression');
const express = require('express');
const helmet = require('helmet');
const xss = require('xss-clean');
const hpp = require('hpp');
const setResponseHeaders = require('./utils/setResponseHeaders');
const logging = require('./utils/loggingMiddleware');
const AppError = require('./utils/appError');
let initializeStatus = 'pending';
const initialize = async () => {
console.log('Start to initialize');
initializeStatus = 'done';
};
exports.initialize = initialize;
const app = express();
// Set security HTTP headers
app.use(helmet());
// Set Encodings
app.use(express.json({ limit: '10kb' }));
app.use(express.urlencoded({ extended: true, limit: '10kb' }));
// Security Middlewares
app.use(mongoSanitize());
app.use(xss());
// Prevent parameter pollution
app.use(hpp());
app.use(comprassion());
// testing middleware
app.use((req, res, next) => {
// On-Demand Test middleware
next();
});
setResponseHeaders.setMiddlewares(app);
if (process.env.NODE_ENV === 'development') logging.setMiddlewares(app);
app.all('*', (req, res, next) => {
if (initializeStatus !== 'done')
return next(
new AppError(
503,
`Service Unacailable, Initialize not completed please try again later`
)
);
next();
});
app.route('/health').get((req, res) =>
res.status(200).json({
status: 'success',
data: {
healthState: 'healthy',
},
})
);
// connect router
// app.use('/order', orderRouter);
app.all('*', (req, res, next) =>
next(
new AppError(
404,
`Can't find ${req.method}:${req.originalUrl} on the instance`
)
)
);
app.use((error, req, res, next) => {
const statusCode = error.statusCode || 500;
if (statusCode >= 500) console.error('Nowhere Error', error.message);
res.status(statusCode).json({
status: statusCode < 500 ? 'failed' : 'error',
message: error.message,
});
});
exports.routes = app;