-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
59 lines (49 loc) · 1.79 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
//requiring core modules
const path = require('path');
//requiring 3rd party modules
const express = require('express');
const morgan = require('morgan');
const rateLimit = 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 cors = require('cors');
// const hpp = require('hpp');
//requiring local modules
const AppError = require('./utils/AppError');
const GlobalErrorHandler = require('./controllers/errorController');
const userRouter = require('./routes/userRoutes');
const postRouter = require('./routes/postRoutes');
const answerRouter = require('./routes/answerRoutes');
const commentRouter = require('./routes/commentRoutes');
const tagRouter = require('./routes/tagRoutes');
//initializing express
const app = express();
app.use(cors({ credentials: true, origin: true }));
//logging on dev environment
// if (process.env.NODE_ENV === 'development') {
// app.use(morgan('dev'));
// }
//middlewares related to secuirity
app.use(helmet());
//initializing body parser and more secuirity middlewares
app.use(express.json({ limit: '20kb' }));
app.use(cookieParser());
app.use(mongoSanitize());
//
//middlewares for routes in api
app.use('/api/v1/posts', postRouter);
app.use('/api/v1/users', xss(), userRouter);
app.use('/api/v1/answers', answerRouter);
app.use('/api/v1/comments', xss(), commentRouter);
app.use('/api/v1/tags', xss(), tagRouter);
//middleware for handling unknown routes
app.all('*', (req, res, next) => {
return next(
new AppError(`Can't find ${req.originalUrl} route on this server!!!`, 404)
);
});
//globar error handler
app.use(GlobalErrorHandler);
module.exports = app;