-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
63 lines (51 loc) · 1.86 KB
/
Copy pathapp.js
File metadata and controls
63 lines (51 loc) · 1.86 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
const express = require("express");
const app = express();
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");
app.use(express.json());
const tourRoute = require("./routes/tourRoute");
const userRoute = require("./routes/userRoute");
const reviewRoute = require("./routes/reviewRoute");
const AppError = require("./utils/appError");
const globalErrorHandler = require("./controller/errorController");
// 🛡️ Adds security headers to protect against common web vulnerabilities.
app.use(helmet());
// 🧹 Sanitizes incoming data to prevent NoSQL injection attacks.
app.use(mongoSanitize());
// 🗡️ Sanitizes incoming data to prevent cross-site scripting (XSS) attacks.
app.use(xss());
// ✅ Protects against HTTP Parameter Pollution, whitelisting essential parameters.
app.use(
hpp({
whitelist: [
"duration",
"ratingsQuantity",
"ratingsAverage",
"maxGroupSize",
"difficulty",
"price",
],
})
);
// Mounting the tour route handler at '/api/v1/tours'
app.use("/api/v1/tours", tourRoute);
app.use("/api/v1/users", userRoute);
app.use("/api/v1/reviews", reviewRoute);
// Creates a rate limiter that allows each IP to make 100 requests per 60 minutes
const limiter = rateLimit({
windowMs: 60 * 60 * 1000,
max: 100,
message: "Too many requests, please try again later.",
});
// 🔐 Limits the number of requests a client can make to the server.
app.use("/api", limiter);
// Handling undefined routes and generating a 404 error ❌🚨
app.all("*", (req, res, next) => {
next(new AppError(`Can't find ${req.originalUrl} on this server`, 404));
});
// Using the global error handler middleware to handle errors throughout the app.
app.use(globalErrorHandler);
module.exports = app;