-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
56 lines (41 loc) · 1.31 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
const express = require('express');
const rateLimit = require('express-rate-limit');
const helmet = require('helmet');
const mongoSanitize = require('express-mongo-sanitize');
const xss = require('xss-clean');
const hpp = require('hpp');
const cors = require('cors');
const userRoutes = require('./routes/userRoutes');
const globalErrHandler = require('./controllers/errorController');
const AppError = require('./utils/appError');
const app = express();
// Allow Cross-Origin requests
app.use(cors());
// Set security HTTP headers
app.use(helmet());
// Limit request from the same API
const limiter = rateLimit({
max: 150,
windowMs: 60 * 60 * 1000,
message: 'Too Many Request from this IP, please try again in an hour'
});
app.use('/', limiter);
// Body parser, reading data from body into req.body
app.use(express.json({
limit: '15kb'
}));
// Data sanitization against Nosql query injection
app.use(mongoSanitize());
// Data sanitization against XSS(clean user input from malicious HTML code)
app.use(xss());
// Prevent parameter pollution
app.use(hpp());
// Routes
app.use('/users', userRoutes);
// handle undefined Routes
app.use('*', (req, res, next) => {
const err = new AppError(404, 'fail', 'undefined route');
next(err, req, res, next);
});
app.use(globalErrHandler);
module.exports = app;