-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
46 lines (37 loc) · 1.76 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
// Import necessary packages/modules
const express = require("express");
const morgan = require("morgan"); // Middleware for request logging
const moment = require("moment"); // Time manipulation library
const rateLimit = require("express-rate-limit"); // Rate limiting middleware
const helmet = require("helmet"); // Security headers middleware
const mongoSanitize = require("express-mongo-sanitize"); // Prevent NoSQL injection
const xss = require("xss-clean"); // Prevent cross-site scripting (XSS) attacks
const hpp = require("hpp"); // Prevent HTTP Parameter Pollution
// Import route handler for user-related routes
const userRouter = require("./routes/userRouter");
// Import global error controller
const globalErrorsController = require("./controllers/errorController");
// Create an Express application instance
const app = express();
// Parse incoming JSON requests
app.use(express.json());
// Request logging using 'morgan' middleware with the 'tiny' format
if (process.env.NODE_ENV === "dev") app.use(morgan("tiny"));
// Apply security-related middlewares
app.use(helmet()); // Set security headers
app.use(mongoSanitize()); // Prevent NoSQL injection
app.use(xss()); // Prevent XSS attacks
app.use(hpp()); // Prevent HTTP Parameter Pollution
// Configure rate limiting for API requests
const limiter = rateLimit({
max: 100, // Maximum number of requests
windowMs: moment.duration(1, "hours").asMilliseconds(), // Reset rate limiting after 1 hour
message: "Too many requests from this user, try again in an hour.",
});
app.use("/api", limiter);
// Mount the userRouter middleware to handle user-related routes
app.use("/api/users", userRouter);
// Global error handling middleware
app.use(globalErrorsController);
// Export the Express application instance
module.exports = app;