-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
103 lines (85 loc) · 3.15 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
const express = require("express");
const techRoutes = require("./routes/technologyRoutes");
const wondersRoutes = require("./routes/wondersRoutes");
const erasQuotesRoutes = require("./routes/erasRoutes");
const allQuotesRoutes = require("./routes/allQuotesRoutes");
const greatWorksRoutes = require("./routes/greatWorksRoutes");
const AppError = require("./utils/appError");
const globalErrorHandler = require("./controllers/errorController");
const cors = require("cors");
const rateLimit = require("express-rate-limit");
const helmet = require("helmet");
const mongoSanitize = require("express-mongo-sanitize");
const hpp = require("hpp");
const xss = require("xss-clean");
const app = express();
/********************************* CORS ***************************** */
/************************************************************************** */
app.use(cors());
/********************************* SECURITY MIDDLEWARE ***************************** */
/************************************************************************** */
app.enable("trust proxy");
app.use(helmet());
app.use(
helmet.contentSecurityPolicy({
directives: {
defaultSrc: ["'self'", "data:", "blob:"],
baseUri: ["'self'"],
fontSrc: ["'self'", "https:", "data:"],
scriptSrc: ["'self'", "https://*.cloudflare.com"],
scriptSrc: ["'self'", "https://*.stripe.com"],
scriptSrc: ["'self'", "http:", "https://*.mapbox.com", "data:"],
frameSrc: ["'self'", "https://*.stripe.com"],
objectSrc: ["'none'"],
styleSrc: ["'self'", "https:", "unsafe-inline"],
workerSrc: ["'self'", "data:", "blob:"],
childSrc: ["'self'", "blob:"],
imgSrc: ["'self'", "data:", "blob:"],
connectSrc: ["'self'", "blob:", "https://*.mapbox.com"],
upgradeInsecureRequests: [],
},
})
);
const limiter = rateLimit({
max: 100, // Req x IP
windowMs: 60 * 60 * 1000, // in 1hs
message: "Too many requests for this IP, try again in an hour.",
});
// With all routes started with '/api'
app.use("/api", limiter);
///////// Data sanitization against NoSQL query injection
app.use(mongoSanitize());
// Data sanitization against XSS
app.use(xss());
//////// Prevent parameter pollution
app.use(
hpp({
whitelist: [
"duration",
"ratingsAverage",
"ratingsQuantity",
"maxGroupSize",
"difficulty",
"price",
],
})
);
/********************************* MIDDLEWARES (API-App) ***************************** */
/************************************************************************** */
app.use(express.json());
app.use((req, res, next) => {
req.reqTime = Date.now();
next();
});
/********************************* ROUTES ******************************** */
/************************************************************************** */
app.use("/api/v1/technologies", techRoutes);
app.use("/api/v1/wonders", wondersRoutes);
app.use("/api/v1/great-works", greatWorksRoutes);
app.use("/api/v1/eras", erasQuotesRoutes);
app.use("/api/v1/all-quotes", allQuotesRoutes);
app.all("*", (req, res, next) => {
next(new AppError(`Can't find ${req.originalUrl} on this server!`, 404));
});
app.use(globalErrorHandler);
module.exports = app;