-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
136 lines (104 loc) · 3.72 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
// Core / Framework / Third-Party Module Imports
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 path = require('path');
const cookieParser = require('cookie-parser');
const compression = require('compression');
const cors = require('cors');
// Local Module Imports
const tourRouter = require('./routes/tourRoutes');
const usersRouter = require('./routes/usersRoutes');
const reviewRouter = require('./routes/reviewRoutes');
const bookingRouter = require('./routes/bookingRoutes');
const bookingController = require('./controllers/bookingController');
const viewRouter = require('./routes/viewRoutes');
const globalErrorHandler = require('./controllers/errorController');
const AppError = require('./utils/appError');
const { application } = require('express');
const app = express();
// Enable the option to trust proxies
app.enable('trust proxy');
// Enable CORS for Cross origin requests
app.use(cors());
// Enable CORS for options like POST / PUT / DELETE
app.options('*', cors());
// Set the app view engine to pug templetes
app.set('view engine', 'pug');
// Set the app engine to look for templates in the Views folder
app.set('views', path.join(__dirname, 'views'));
// Rate Limiter
const limiter = rateLimit({
max: 1000,
windowMs: 60 * 60 * 1000,
message: 'Too Many requests. Please Try again in an Hour.',
});
// GLOBAL MIDDLEWARES
// To serve static files
app.use(express.static(path.join(__dirname, 'public')));
// MIDDLEWARES / NPM PACKAGES
// To set Secure HTTP headers
app.use(
helmet({
contentSecurityPolicy: false,
}),
);
// To limit the number of requests coming from a single IP
app.use(limiter);
// Route for the stripe webhook checkout
app.post(
'/stripe-webhook-checkout',
express.raw({ type: 'application/json' }),
bookingController.stripeWebhookCheckout,
);
// Body parser / To read the data from the request Body
app.use(express.json({ limit: '10kb' }));
// Cookie Parser / To read the incoming cookie
app.use(cookieParser());
// URL Parser / To decode the incoming data that is encoded in a url and parse the data
app.use(express.urlencoded({ extended: true, limit: '10kb' }));
// Preventing NOSQL Query Injection
app.use(mongoSanitize());
// Preventing XSS Attacks
app.use(xss());
// Prevent Parameter Pollution
app.use(
hpp({
whitelist: ['duration', 'price', 'difficulty', 'maxGroupSize', 'ratingsAverage', 'ratingsQuantity'],
}),
);
// To log all Requests during Development
if (process.env.NODE_ENV === 'development') {
app.use(morgan('dev'));
}
// Response compression middleware
app.use(compression());
// CUSTOM MIDDLEWARES
// Middleware to capture the time of Request.
app.use((req, res, next) => {
const date = new Date();
req.time = `Date: ${date.getDate()}-${date.getMonth()}-${date.getFullYear()}, Time: ${date.getHours()}:${date.getMinutes()}:${date.getSeconds()}:${date.getMilliseconds()}`;
next();
});
/* Router Mounting */
// Views Router
app.use('/', viewRouter);
// Tour Router
app.use('/api/v1/tours', tourRouter);
// Users Router
app.use('/api/v1/users', usersRouter);
// Review Routes
app.use('/api/v1/reviews', reviewRouter);
// Booking / Checkout Routes
app.use('/api/v1/booking', bookingRouter);
// Router to handle UNDEFINED Routes
app.all('*', (req, res, next) => {
next(new AppError(`The Requested ${req.originalUrl} was not found on the Server`, 404));
});
app.use(globalErrorHandler);
// Export app
module.exports = app;