-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
66 lines (55 loc) · 1.49 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
const express = require("express")
const fs = require("fs")
const morgan = require("morgan");
const tourRouter = require("./routes/tourRoutes");
const userRouter = require("./routes/userRoutes");
const AppError = require("./utils/appError");
const rateLimiter = require("./utils/rateLimiter");
const helmet = require("helmet")
const mongoSanitize = require("express-mongo-sanitize");
const xssClean = require("xss-clean");
const hpp = require("hpp");
const reviewRouter = require("./routes/reviewRoute");
const app = express();
app.use(express.json({ limit: "10kb" }))
//! middleware
app.use(morgan("dev"))
//! SECURITY
app.use("/api", rateLimiter);
app.use(helmet());
app.use(
mongoSanitize({
replaceWith: '_',
}),
);
app.use(xssClean());
app.use(hpp({
whitelist: [
"duration",
"difficulty",
"price"
]
}))
app.use((req, res, next) => {
req.requestTime = new Date().toDateString();
next();
})
//routes
app.use("/api/v1/tours", tourRouter)
app.use("/api/v1/users", userRouter)
app.use("/api/v1/review", reviewRouter)
//! undefined route - error management
app.all("*", (req, res, next) => {
next(new AppError("Undefined path, check your url!", 404))
})
//! GLOBAL ERROR MANAGEMENT
app.use((err, req, res, next) => {
err.statusCode = err.statusCode || 500;
err.status = err.status || "error"
res.status(err.statusCode).json({
status: err.status,
message: err.message
})
console.log(err.stack);
})
module.exports = app