-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
88 lines (75 loc) · 2.26 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
const express = require('express');
const morgan = require('morgan');
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 AppError = require('./utilities/appError');
const globalErrorHandler = require('./controllers/errorController');
const tourRouter = require('./routes/tourRoute');
const userRouter = require('./routes/userRoute');
const app = express();
/// 1.)Middlewares
// console.log(process.env.NODE_ENV);
//Global Middlewares
///Securing HTTP routes using Multiple headers
app.use(helmet());
///using Morgan just for DEVs
if (process.env.NODE_ENV === 'development') {
app.use(morgan('dev'));
}
///Limit requests from the same API
const limiter = rateLimit({
max: 100,
windowMs: 60 * 60 * 1000,
message: 'Too many requests from this IP, please try again later',
});
app.use('/api', limiter);
app.use(express.json({ limit: '10kb' })); ///body-parser for incoming json
///data sanatisation against NOSQL query injection
app.use(mongoSanitize());
//data sanatisation against XSS
app.use(xss());
/// removing the HTTP parameters pollution
app.use(
hpp({
whitelist: [
'duration',
'ratingsAverage',
'ratinsQuantitiy',
'maxGroupSize',
'difficulty',
'price',
],
})
);
app.use(express.static(`${__dirname}/public`)); //static html
//Test middleware
app.use((req, res, next) => {
req.requestTime = new Date().toISOString();
next();
});
///routes as middleware
app.use('/api/v1/tours', tourRouter);
app.use('/api/v1/users', userRouter);
app.all('*', (req, res, next) => {
next(new AppError(`Can't find ${req.originalUrl} on this server!`, 404));
});
app.use(globalErrorHandler);
module.exports = app;
///routing using express
///get
// app.get('/', (req, res) => {
// res.status(200).send('Hello from the other side....');
// });
// app.get('/', (req, res) => {
// res
// .status(200)
// .json({ message: 'Hello from the other side....', app: 'natours' });
// });
// /////post
// app.post('/', (req, res) => {
// res.send('You can sent to this endpoint');
// });
// console.log(tours);