-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
208 lines (183 loc) · 5.31 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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
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 compression = require('compression');
const tourRoutes = require('./routers/tourRouters');
const userRoutes = require('./routers/userRouters');
const reviewRoutes = require('./routers/reviewRouters');
const bookingRoutes = require('./routers/bookingRouters');
const viewRoutes = require('./routers/viewRouters');
const globalErrorHandler = require('./Controller/errorController');
const AppError = require('./utils/appError');
//Start express App
const app = express();
app.enable('trust proxy');
//Use template pug
app.set('view engine', 'pug');
app.set('views', path.join(__dirname, 'views'));
//1. Global Middleware
// Set security HTTP headers
app.use(helmet());
//Development logging basically
if (process.env.NODE_ENV === 'development') {
app.use(morgan('dev'));
}
//Limit requests from same API
const limiter = rateLimit({
max: 50,
windowMs: 60 * 60 * 1000,
message: 'Too many requests from this IP , please try again in an hour💥!',
});
app.use('/api', limiter);
//Body perser,Reading data from body into req.body
app.use(express.json({
limit: '10kb'
}));
app.use(express.urlencoded({
extended: true,
limit: '10kb'
}));
app.use(cookieParser());
//Data sanitize against NoSQL query Injection
app.use(mongoSanitize());
//Data sanitize against XSS
app.use(xss());
//Prevent parameter pollution
app.use(
hpp({
whitelist: [
'duration',
'ratingsAverage',
'ratingsQuantity',
'maxGroupSize',
'difficulty',
'price',
],
})
);
//Serving Static Files
// app.use(express.static(`${__dirname}/public`));
app.use(express.static(path.join(__dirname, 'public')));
// app.use((req, res, next) => {
// // eslint-disable-next-line no-console
// console.log('This is middleware apps');
// next();
// });
app.use(compression());
//Test Middleware
app.use((req, res, next) => {
req.requestTime = new Date().toISOString();
// console.log(req.cookies);
next();
});
// Middleware is basically a function that can modify incomming request data.
// app.get('/', (req, res) => {
// res.status(200)
// .json({
// message: 'Hello from the server site.👌',
// app: 'Natours'
// });
// });
// app.post('/',(req, res)=>{
// res.status(200).send('You can POST to this endpoint....');
// });
//2.Routing
app.use('/', viewRoutes);
app.use('/api/v1/tours', tourRoutes);
app.use('/api/v1/users', userRoutes);
app.use('/api/v1/reviews', reviewRoutes);
app.use('/api/v1/bookings', bookingRoutes);
app.all('*', (req, res, next) => {
next(new AppError(`Can't find ${req.originalUrl} on this server.`, 404));
});
app.use(globalErrorHandler);
//app.get('/api/v1/tours', getAllTours);
//app.get('/api/v1/tours/:id', getTours);
//app.post('/api/v1/tours', createNewTour);
//app.patch('/api/v1/tours/:id', updateTour);
//app.delete('/api/v1/tours/:id', deleteTour);
// app.get('/api/v1/tours', (req, res) => {
// res.status(200).json({
// status: 'success',
// total_tour: tours.length,
// data: {
// tours
// }
// });
// });
// app.get('/api/v1/tours/:id', (req, res) => {
// console.log(req.params);
// const id = req.params.id * 1;
// // if (id > tours.length) {
// // return res.status(404).json({
// // status: 'fail',
// // message: "Invalid ID",
// // });
// // }
// const tour = tours.find(el => el.id === id);
// if (!tour) {
// return res.status(404).json({
// status: 'fail',
// message: "Invalid ID",
// });
// }
// res.status(200).json({
// status: 'success',
// // total_tour: tours.length,
// data: {
// tour
// }
// });
// });
// app.post('/api/v1/tours', (req, res) => {
// // console.log(req.body);
// // res.send('Done');
// const newID = tours[tours.length - 1].id + 1;
// const newTour = Object.assign({
// id: newID
// }, req.body);
// tours.push(newTour);
// fs.writeFile(`${__dirname}/dev-data/data/tours-simple.json`, JSON.stringify(tours), err => {
// res.status(201).json({
// status: 'success',
// data: {
// tours: newTour
// }
// })
// console.log(err);
// })
// });
// app.patch('/api/v1/tours/:id', (req, res) => {
// if (req.params.id > tours.length) {
// return res.status(404).json({
// status: 'fail',
// message: "Invalid ID",
// });
// }
// res.status(200).json({
// status: 'success',
// data: {
// tour: '<Updated tour here>'
// }
// });
// });
// app.delete('/api/v1/tours/:id', (req, res) => {
// if (req.params.id > tours.length) {
// return res.status(404).json({
// status: 'fail',
// message: "Invalid ID",
// });
// }
// res.status(204).json({
// status: 'success',
// data: null
// });
// });
//3.Localhost
module.exports = app;