-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
62 lines (49 loc) · 1.5 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
const express = require("express");
const morgan = require("morgan");
// const dotenv = require("dotenv");
const rateLimiter = require("express-rate-limit");
const helmet = require("helmet");
const mongoSanitize = require("express-mongo-sanitize");
const xss = require("xss-clean");
const cookieParser = require("cookie-parser");
const errorController = require("./controllers/errorController");
const productRouter = require("./routes/productRoutes");
const userRouter = require("./routes/userRoutes");
const AppError = require("./utils/appError");
const app = express();
// SETTING UP ENVIRONMENT VARIABLES
// dotenv.config({ path: "./config.env" });
// MIDDLEWARES
// Set security http
app.use(helmet());
// Limit requests per IP to 100 per hour
const limiter = rateLimiter({
max: 100,
windowsMs: 60 * 60 * 1000,
messgae: "Too many requests from this IP, please try again in an hour!",
});
app.use("/api", limiter);
if (process.env.NODE_ENV === "development") {
app.use(morgan("dev"));
}
// Parse requests
app.use(express.json({ limit: "10kb" }));
app.use(cookieParser());
// Data sanitization against NoSQL injection and xss
app.use(mongoSanitize());
// app.use(xss());
// ROUTES
app.use("/api/v1/products", productRouter);
app.use("/api/v1/users", userRouter);
// Handle not found routes
app.all("*", (req, res, next) => {
next(
new AppError(
404,
`This route ${req.originalUrl} deos not exist on this server`
)
);
});
// ERROR HANDLING
app.use(errorController);
module.exports = app;