-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
47 lines (33 loc) · 1.17 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
const express = require('express');
const morgan = require('morgan');
const helmet = require('helmet');
const xss = require('xss-clean');
const mongoSanitize = require('express-mongo-sanitize');
const AppError = require('./Utils/appError.js');
const globalErrorHandler = require('./controllers/ErrorController');
const courseRouter = require('./routes/courseRoutes');
const userRouter = require('./routes/userRoutes');
const app = express();
// 1) GLOBAL MIDDLEWARES
// Set Security HTTP Headers
app.use(helmet());
// For development logging
if (process.env.NODE_ENV === 'development') {
app.use(morgan('dev'));
}
// Body Parser, Reading Data from body
app.use(express.json({ limit: '10kb' }));
// Data Sanitization against NoSQL Query Injection
app.use(mongoSanitize());
// Data sanitization against xss
app.use(xss());
// 2) ROUTES
app.use('/api/v1/course', courseRouter);
app.use('/api/v1/users', userRouter);
// After this If request and response cycle has not ended then
// It means that it is unhandled request
app.all('*',(req,res,next) => {
next(new AppError(`Can't find ${req.originalUrl} On this Server`,404));
});
app.use(globalErrorHandler);
module.exports = app;