-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
61 lines (46 loc) · 1.54 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
const express = require('express')
const app = express()
const cookieParser = require('cookie-parser')
const dotenv = require('dotenv')
const rateLimiter = require('express-rate-limit')
const helmet = require('helmet')
const sanitize = require('express-mongo-sanitize')
const xss = require('xss-clean')
const hpp = require('hpp')
//Controllers
const errorController = require('./controllers/errorController')
//Routers
const userRouter = require('./routes/userRouter')
const reviewRouter = require('./routes/reviewRouter')
const productRouter = require('./routes/productRouter')
//Loading contants and modules
dotenv.config('./.env')
require('./db')()
const limiter = rateLimiter({
max: 100,
windowMs: 3600000,
message: 'Too many requests from this ip adress. Please try again in one hour!'
});
//Limit number of requests to 100 for certain ip adress
app.use('/users', limiter);
//Set security headers for additional protection
app.use(helmet());
//Convert request to json data and limit to 10kb
app.use(express.json({ limit: '10kb'}));
app.use(express.urlencoded({
extended: false
}));
//Parse cookies for later use
app.use(cookieParser())
//Protect mongo from no sql query injection
app.use(sanitize())
//Protect app from xss
app.use(xss())
//Protect from parameter polution
app.use(hpp());
app.use('/api/v1/users', userRouter);
app.use('/api/v1/reviews', reviewRouter);
app.use('/api/v1/products', productRouter);
app.all('*', errorController.wrongRouteHitHandler);
app.use(errorController.rejectedPromiseHandler);
module.exports = app;