-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
81 lines (64 loc) · 2.25 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
const express = require('express');
const morgan = require('morgan');
const path = require('path');
const rateLimit = require('express-rate-limit');
const helmet = require('helmet');
const xss = require('xss-clean');
const mongoSanitize = require('express-mongo-sanitize');
const hpp = require('hpp');
const cookieParser = require('cookie-parser');
const useragent = require('express-useragent');
const compression = require('compression');
const cors = require('cors');
const AppError = require('./utils/appError');
const globaleErrorHandler = require('./controllers/errorController');
const eventRouter = require('./routes/eventRoutes');
const foodtruckRouter = require('./routes/foodtruckRoutes');
const geoRouter = require('./routes/geoRoutes');
const userRouter = require('./routes/userRoutes');
const testRouter = require('./routes/testRoutes');
const app = express();
app.set('view engine', 'pug');
app.set('views', path.join(__dirname, 'views'));
// Implement CORS
app.use(cors());
app.options('*', cors());
// Display static files
app.use(express.static(path.join(__dirname, 'public')));
// GLOBAL MIDDLEWARES
// Helmet -- set security HTTP headers
app.use(helmet());
// Morgan -- development logging
if (process.env.NODE_ENV === 'development') {
app.use(morgan('dev'));
}
// Express-Rate-Limit -- limit requests from same API
const limiter = rateLimit({
max: 20,
windowMs: 60 * 60 * 1000,
message: 'Too many requests from this IP, please try again in an hour.'
});
app.use('/api', limiter);
// Body Parser -- reading data from the body into req.body
app.use(express.json({ limit: '10kb' }));
app.use(express.urlencoded({ limit: '10kb', extended: true }));
app.use(cookieParser());
// Data Sanitization
// -- against NoSQL query injection
app.use(mongoSanitize());
// -- against XSS
app.use(xss());
// HPP -- prevent parameter pollution
// nothing yet
// app.use(compression());
// Routers
app.use('/api/v1/veniceevents', eventRouter);
app.use('/api/v1/foodtrucks', foodtruckRouter);
app.use('/api/v1/geos', geoRouter);
app.use('/api/v1/admins', userRouter);
app.use('/api/v1/testApi', testRouter);
app.all('*', (req, res, next) => {
next(new AppError(`Could not find ${req.originalUrl} on this server!`, 404));
});
app.use(globaleErrorHandler);
module.exports = app;