-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
124 lines (106 loc) · 3.96 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/* eslint-disable import/no-extraneous-dependencies */
const express = require('express');
const morgan = require('morgan');
const cors = require('cors');
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 path = require('path');
const subscribersRouter = require('./routes/subscribersRoute');
const AppError = require('./utils/appError');
const globalErrorHandler = require('./controllers/errorController');
const researchRouter = require('./routes/researchRoute');
const shareholderRouter = require('./routes/shareholderRoute');
const reportRouter = require('./routes/docsAndReportsRoute');
const shareValueRouter = require('./routes/shareValueRoute');
const investorRouter = require('./routes/investorRoute');
const ordersRouter = require('./routes/ordersRoute');
const investmentsRouter = require('./routes/investmentRoute');
const roiValueRouter = require('./routes/roiValueRoute');
const businessNewsRouter = require('./routes/businessNewsRoute');
const fundPerfomanceCommentaryRouter = require('./routes/fundPerformanceCommentaryRoute');
const investmentPositionRouter = require('./routes/investmentPositionRoute');
const featuredPostRouter = require('./routes/featuredPostRoute');
const adminRouter = require('./routes/adminRoute');
// start the express app
const app = express();
app.set('trust proxy', 300);
app.get('/x-forwarded-for', (request, response) =>
response.send(request.headers['x-forwarded-for']),
);
// set security http headers
app.use(helmet());
app.use(cors());
// using a body parser
app.use(express.json());
// app.use(express.static(`${__dirname}/public`));
// setting the static directory
app.use(express.static(path.join(__dirname, 'public')));
// app.use((req, res, next) => {
// res.header('Access-Control-Allow-Origin', '*');
// res.header(
// 'Access-Control-Allow-Headers',
// 'Content-Type,Content-Length, Authorization, Accept,X-Requested-With',
// );
// res.header('Access-Control-Allow-Methods', 'PUT,POST,GET,DELETE,OPTIONS');
// next();
// });
if (process.env.NODE_ENV === 'development') {
app.use(morgan('dev'));
}
// Data sanitization against no-sql query inject and cross site attacks
// prevents query injections
app.use(mongoSanitize());
// limit requests from api
const limiter = rateLimit({
max: 3000,
windowMs: 60 * 60 * 1000,
message: 'Too many requests from this IP, please try again in an hour.',
});
app.use('/api', limiter);
// limit to body parser, reading data from body in req.body
app.use(express.json({ limit: '10kb' }));
// app.use(cookieParser());
// prevents xss attacks
app.use(xss());
// prevents parameter pollution
// try getting the field names from the model
app.use(
hpp({
whitelist: [
'duration',
'ratingsQuantity',
'ratingsAverage',
'maxGroupSize',
'difficulty',
'price',
],
}),
);
app.use((req, res, next) => {
req.requestTime = new Date().toISOString();
// console.log(req.cookies);
next();
});
app.use('/api/v1/subscribers', subscribersRouter);
app.use('/api/v1/researches', researchRouter);
app.use('/api/v1/shareholders', shareholderRouter);
app.use('/api/v1/reports', reportRouter);
app.use('/api/v1/sharevalue', shareValueRouter);
app.use('/api/v1/investors', investorRouter);
app.use('/api/v1/orders', ordersRouter);
app.use('/api/v1/investments', investmentsRouter);
app.use('/api/v1/roivalue', roiValueRouter);
app.use('/api/v1/business-news', businessNewsRouter);
app.use('/api/v1/fundPerformanceCommentaries', fundPerfomanceCommentaryRouter);
app.use('/api/v1/investmentPositions', investmentPositionRouter);
app.use('/api/v1/featured-posts', featuredPostRouter);
app.use('/api/v1/admin', adminRouter);
app.use('*', (req, res, next) => {
next(new AppError(`Can't find ${req.originalUrl} at the moment`, 404));
});
app.use(globalErrorHandler);
module.exports = app;