-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
87 lines (71 loc) · 2.12 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
/**
* @fileoverview Main application file for setting up and configuring the Express app.
* This file includes middleware setup, route handling, and global error handling.
*/
import express from 'express';
import morgan from 'morgan';
import rateLimit from 'express-rate-limit';
import helmet from 'helmet';
import mongoSanitize from 'express-mongo-sanitize';
import hpp from 'hpp';
import GlobalErrHandler from './controllers/errorController.js';
import AppError from './utils/appError.js';
// import cookieParser from 'cookie-parser';
// Routers
// import viewRouter from './routes/viewRoutes.js';
import entertainmentRouter from './routes/entertainmentRoutes.js';
import userRouter from './routes/userRoutes.js';
import reviewRouter from './routes/reviewRoutes.js';
import watchlistRouter from './routes/watchlistRoutes.js';
const app = express();
/**
* Middleware setup
*/
// Sets security HTTP headers
app.use(helmet());
// Logging middleware for development environment
if (process.env.NODE_ENV === 'development') {
app.use(morgan('dev'));
}
// Rate limiter middleware to prevent excessive requests from the same IP
const limiter = rateLimit({
max: 100,
windowMs: 60 * 60 * 1000, // 1 hour
message: 'Too many requests from this IP, please try again in an hour!',
});
app.use('/api', limiter);
// Body parser middleware to handle JSON payloads
app.use(express.json({ limit: '10kb' }));
// Data sanitization against NoSQL query injection
app.use(mongoSanitize());
// Prevent HTTP Parameter Pollution
app.use(
hpp({
whitelist: [
'name',
'yearOfRelease',
'imdbRating',
'type',
'genre',
'duration',
'rated',
],
})
);
/**
* Route handlers
*/
// app.use('/', viewRouter);
app.use('/api/v1/entertainment', entertainmentRouter);
app.use('/api/v1/users', userRouter);
app.use('/api/v1/reviews', reviewRouter);
app.use('/api/v1/watchlist', watchlistRouter);
// Handle unhandled routes
app.all('*', (req, res, next) => {
next(new AppError(`Cannot find ${req.originalUrl} on this server.`, 404));
});
/**
* Global error handling middleware
*/
app.use(GlobalErrHandler);
export default app;