forked from zero-to-mastery/GrowersBrains
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
67 lines (55 loc) · 2.32 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
const express = require('express');
const morgan = require('morgan');
const helmet = require('helmet');
const rateLimit = require('express-rate-limit');
const mongoSanitize = require('express-mongo-sanitize');
const xss = require('xss-clean');
const AppError = require('./utils/appError');
const globalErrorHandler = require('./controllers/errorController');
const plantRouter = require('./routes/plantRoutes');
const productRouter = require('./routes/productRoutes');
const greenhouseRouter = require('./routes/greenhouseRoutes');
const userRouter = require('./routes/userRoutes');
const articleRouter = require('./routes/articleRoutes');
const reviewArticleRouter = require('./routes/reviewArticleRoutes');
const reviewGreenhouseRouter = require('./routes/reviewGreenhouseRoutes');
const jobRouter = require('./routes/jobRoutes');
const questionRouter = require('./routes/questionRoutes');
const answerRouter = require('./routes/answerRoutes');
const app = express();
//Devlopment middleware
if (process.env.NODE_ENV === 'development') {
app.use(morgan('dev'));
}
//Set Security HTTP headers
app.use(helmet());
//Limit requests from the same API
const limiter = rateLimit({
windowMs: 60 * 60 * 1000, // 1 hour
max: 100, // allow 100 requests from the same IP for 1 hour
message: 'Too Many request from that IP ! Please try again after 1 hour.',
});
app.use('/api', limiter); // apply the limiter middleware for all the routes that starts with `/api`
//Body Parser; reading the data from the body to req.body(max 15kb)
app.use(express.json({ limit: '15kb' }));
//Data sanitization , against NOSQL query injection
app.use(mongoSanitize());
//Data sanitization against XSS
app.use(xss());
//ROUTES
app.use('/api/v1/plants', plantRouter);
app.use('/api/v1/users', userRouter);
app.use('/api/v1/products', productRouter);
app.use('/api/v1/greenhouses', greenhouseRouter);
app.use('/api/v1/articles', articleRouter);
app.use('/api/v1/reviews-article', reviewArticleRouter);
app.use('/api/v1/reviews-greenhouse', reviewGreenhouseRouter);
app.use('/api/v1/jobs', jobRouter);
app.use('/api/v1/questions', questionRouter);
app.use('/api/v1/answers', answerRouter);
app.all('*', (req, res, next) => {
next(new AppError(`Can't find ${req.originalUrl} on this server`, 404));
});
//GLOBAL ERROR HANDLER MIDDLEWARE
app.use(globalErrorHandler);
module.exports = app;