-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
97 lines (77 loc) · 1.96 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
87
88
89
90
91
92
93
94
95
96
97
const path = require('path');
const express = require('express');
const morgan = require('morgan');
const helmet = require('helmet');
const hpp = require('hpp');
const xss = require('xss-clean');
const mongoSanitize = require('express-mongo-sanitize');
const rateLimit = require('express-rate-limit');
const cookieParser = require('cookie-parser');
const compression = require('compression');
const globalErrorHandler = require('./controllers/errorController');
const AppError = require('./utils/appError');
const userRouter = require('./routes/userRoutes');
const app = express();
// 1) Middlewares
app.set('view engine', 'pug');
app.set('views', path.join(__dirname, 'views'));
/**
* serving static files
*/
app.use(express.static(path.join(__dirname, 'public')));
/**
* set security http headers
*/
app.use(helmet());
// A) General Middleware
/**
* Body parser, reading data from body into req.body
*/
app.use(express.json({ limit: '10kb' }));
app.use(express.urlencoded({ extended: true, limit: '10kb' }));
/**
* Parse cookies
*/
app.use(cookieParser());
/**
* log requests in Dev
*/
if (process.env.NODE_ENV === 'development') app.use(morgan('dev'));
// B) Other Security Middleware
/**
* Limit requests from same IP
*/
const limiter = rateLimit({
max: 100,
windowMs: 60 * 60 * 1000,
message: 'Too many requests from this IP, please try again in an hour!',
});
app.use('/api', limiter);
/**
* Data sanitization against NoSQL query Injection
*/
app.use(mongoSanitize());
/**
* Avoid http parameter pollution
*/
app.use(
hpp({
whitelist: ['price'],
})
);
/**
* Avoid xss injection
*/
app.use(xss());
// C) Compression using gzip
app.use(compression());
// 2) Routes
app.use('/api/v1/users', userRouter);
// catch 404 and forward to error handler
app.use(function (req, res, next) {
next(new AppError(`Can't find ${req.originalUrl} on this server`, 404));
});
// 3) Global Error Handler
app.use(globalErrorHandler);
// 4) exports app
module.exports = app;