-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
131 lines (107 loc) · 4.16 KB
/
Copy pathapp.js
File metadata and controls
131 lines (107 loc) · 4.16 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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import express from "express";
import morgan from "morgan";
import rateLimit from "express-rate-limit";
import helmet from "helmet";
import './src/config/config.js';
import AppError from './src/utils/appErrors.js';
import globalErrorHandler from './src/Helpers/globalErrorHandler.js';
import { router as TourRoute } from "./src/routes/tourRoute.js";
import { router as AuthRoute } from "./src/routes/authRoute.js";
import { router as UserRoute } from "./src/routes/userRoute.js";
import { router as OrderRoute } from "./src/routes/orderRoute.js";
import { router as OrderItemRoute } from "./src/routes/orderItemRoute.js";
import { router as ProductRoute } from "./src/routes/productRoute.js";
import { router as ReviewRoute } from "./src/routes/reviewRoute.js";
import { router as BookingRoute } from "./src/routes/bookingRoute.js";
import { router as ViewRoute } from "./src/routes/viewRoute.js";
import cookieParser from "cookie-parser";
import { fileURLToPath } from 'url';
import { dirname } from 'path';
import path from 'path';
import mongooseSanitize from 'express-mongo-sanitize';
import xss from 'xss-clean';
import hpp from 'hpp';
import compression from "compression";
import cors from 'cors';
import { log } from "console";
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
const app = express();
// set view engine
app.set('view engine', 'pug');
app.set('views', path.join(__dirname, './src/views'));
// app.set('views', './src/views');
// Global middlewares
// Implement CORS( Cross-origin resource sharing )
app.use(cors());
// Access-Control-Allow-Origin *
// to restrict CORS to some specific domain
// app.use(cors({
// origin: `${process.env.BASE_URL}`
// }));
// all route are allow to requests get, post and also complex request like put and delete
app.options('*', cors());
// enable CORS on particular route
// app.options('/api/v1/tours/:id', cors());
// Set security HTTP headers
app.use(helmet());
// Development logging
app.use(morgan('dev'));
// Body parser, reading data from body into req.body
// app.use(express.json());
app.use(express.json({ limit: '10kb' })); // limit to 10kb for requests body so more then 10k data in request body is not allowed
app.use(express.urlencoded({ extended: true, limit: '10kb' }));
// app.enable('trust proxy');
app.use(cookieParser())
// Data sanitization against request NoSql query injection
app.use(mongooseSanitize());
// Data sanitization against XSS
app.use(xss());
// Prevent parameter pollution
app.use(hpp({
whitelist: [
'duration',
'ratingsQuantity',
'ratingsAverage',
'maxGroupSize',
'difficulty',
'price',
]
}));
// whitelisted parameters those can be repeated
// to send compressed response to client
app.use(compression());
// serving static files
// app.use(express.static(`${__dirname}/public`));
app.use(express.static(path.join(__dirname, './src/public')));
// Limit requests from same API
const limiter = rateLimit({
max: 100, // maximum rate limit for requests is 100
windowMs: 60 * 60 * 1000, // number of milliseconds
message: "Too many requests from this IP address, please try again in an hour!"
});
// means in 1h window max 100 requests are allowed
app.use('/api', limiter);
//route middlewares
app.use('/', ViewRoute);
app.use('/api/v1/tours', TourRoute);
app.use('/api/v1/auth', AuthRoute);
app.use('/api/v1/users', UserRoute);
app.use('/api/v1/orders', OrderRoute);
app.use('/api/v1/orderItems', OrderItemRoute);
app.use('/api/v1/products', ProductRoute);
app.use('/api/v1/reviews', ReviewRoute);
app.use('/api/v1/bookings', BookingRoute);
app.all('*', (req, res, next) => {
next(new AppError(`Can't find ${req.originalUrl} on this server!`, 404));
// res.status(404).json({
// status: 'fail',
// message: `Can't find ${req.originalUrl} on this server!`
// });
// const err = new Error(`Can't find ${req.originalUrl} on this server!`);
// err.status = 'fail';
// err.statusCode = 404;
// next(err);
});
app.use(globalErrorHandler);
export default app;