-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
60 lines (51 loc) · 1.85 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
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 cookieParser = require('cookie-parser');
const userRouter = require(`${__dirname}/routes/userRoutes`);
const viewRouter = require(`${__dirname}/routes/viewRoutes`);
const postRouter = require(`${__dirname}/routes/postRoutes`);
const storyRouter = require(`${__dirname}/routes/storyRoutes`);
const chitChatRouter = require(`${__dirname}/routes/chitChatRoutes`);
const AppError = require('./utils/appError');
const globalErrorHandler = require('./Controller/errorController');
const bodyParser = require('body-parser');
const app = express();
// setting secure header
app.use(helmet());
//it allow max request per windowMs request to server from an ip
const limiter = rateLimit({
max: 100,
windowMs: 60 * 60 * 1000,
message: 'Too many request from an IP, try again later in on hour',
});
app.use('/v1', limiter);
app.use(bodyParser.urlencoded({ extended: true }));
app.use(express.json({ limit: '10kb' }));
app.use(cookieParser());
//data sanization against noSQL query injections
app.use(mongoSanitize());
//data sanitization from xss
app.use(xss());
//preventing from parameter pollution, removes duplicates query
app.use(
hpp({
whitelist: [], // pass parameter for which duplicates are allowed
})
);
app.use(express.static(`${__dirname}/public`));
//routes
app.use('/', viewRouter);
app.use('/v1/story/', storyRouter);
app.use('/v1/users/', userRouter);
app.use('/v1/posts/', postRouter);
app.use('/v1/chitChat/', chitChatRouter);
app.all('*', (req, res, next) => {
next(new AppError(`Can't find ${req.originalUrl} on this Server`, 404));
});
// Error Handling Middleware
app.use(globalErrorHandler);
module.exports = app;