-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
107 lines (95 loc) · 3.15 KB
/
app.js
File metadata and controls
107 lines (95 loc) · 3.15 KB
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
98
99
100
101
102
103
104
105
106
107
const path = require('path');
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 cookieParser = require('cookie-parser');
const AppError = require('./utils/AppError');
const globalErrorHandar = require('./controllers/errorController');
const tourRouter = require('./routes/tourRoutes');
const userRouter = require('./routes/userRoutes');
const reviewRouter = require('./routes/reviewRoutes');
const viewRouter = require('./routes/viewRoutes');
const app = express();
//MIDDLEWARES
//Security middleware helmet
app.use(helmet());
//A middleware that is convering the body objects to Json and vice versa
app.use(express.json({ limit: '50kb' }));
//Parse cookie from the request
app.use(cookieParser());
//Custom Middleware
app.use((req, res, next) => {
req.requestTime = new Date().toISOString();
console.log(req.cookies);
next();
});
//Mongo sannitizer: removes $ and other charatcers with special query character operators
app.use(mongoSanitize());
//Middleware for cross site scripting: removal of malicious html tag based contents in data
app.use(xss());
//Parameter pollution. Cleans up the query parameter with duplicate fields e.g sort=duration&sort=price.
//Whitelist skips the fields from deduplication
app.use(
hpp({
whitelist: [
'duration',
'ratingsAverage',
'ratingsQuantity',
'maxGroupSize',
'difficulty',
'price',
],
})
);
//Express rate limiter
const noOfAPICalls = 2000;
const rateLimiter = rateLimit({
max: noOfAPICalls,
windowMs: 1000 * 60 * 60, //1 Hr window
message: `Too many requests. Limit ${noOfAPICalls} calls in 1 hr`,
});
app.use('/api', rateLimiter);
//Thirdparty Middleware for logging
if (process.env.NODE_ENV === 'development') {
app.use(morgan('dev'));
}
//VIEW ENGINE SETTINGS
app.set('view engine', 'pug');
app.set('views', path.join(__dirname, 'views'));
//make the public directory avl for serving static file
app.use(express.static(path.join(__dirname, 'public')));
//Routes
//templated site pages
app.use('/', viewRouter);
//API routes
app.use('/api/v1/tours', tourRouter);
app.use('/api/v1/users', userRouter);
app.use('/api/v1/reviews', reviewRouter);
//Handle all undefined URL
app.all('*', (req, res, next) => {
// // We can send a direct response
// res.status(400).json({
// status: 'fail',
// message: `Method '${req.method}' or Resource '${req.originalUrl}' unsupported`,
// });
// //We can throw and Error object
// const err = new Error(
// `Method '${req.method}' or Resource '${req.originalUrl}' unsupported`
// );
// err.status = 'fail';
// err.statusCode = 404;
// //Or we can construct a custom Error object
const err = new AppError(
`Method '${req.method}' or Resource '${req.originalUrl}' unsupported`,
404
);
//passing parameter in next() assumes that an error is being passed and is delivered to the error handling middleware
next(err);
});
//Error handling middleware
app.use(globalErrorHandar);
module.exports = app;