forked from NuniTelo/roomie
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
75 lines (60 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
const express = require('express');
const app = 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 hpp = require('hpp');
const cookieParser = require('cookie-parser');
const AppError = require('./utils/appError');
const globalErrorHandler = require('./controllers/errorController');
const listingRouter = require('./routes/listingRouter');
const userRouter = require('./routes/userRouter');
const path = require('path');
app.enable('trust proxy');
// [GLOBAL MIDDLEWARES]
// Development logging
if (process.env.NODE_ENV === 'development') {
app.use(morgan('dev')); //HTTP Request logger
}
// {SECURITY} - Set security HTTP Headers | Best practice is to put it on top
app.use(helmet());
// {SECURITY} - Limit requests from the same IP
const limiter = rateLimit({
max: 1500,
windowMs: 60 * 60 * 1000,
message: 'Too many request from this IP, please try again in an hour',
}); // 1500 Requestes max from the same IP in 1 hour
app.use('/api', limiter);
// {SECURITY} - Data sanitization against NoSQL query injection
app.use(mongoSanitize()); //Remove "$" and others from inputs
// {SECURITY} - Data sanitization against XSS
app.use(xss()); //Can't insert html/javascript into our code
// {SECURITY} - Preventing Parameter Pollution
app.use(
hpp({
whitelist: ['rent'],
})
); //To clear up the query string
// Body parse (reading data from body into req.body)
app.use(express.json({ limit: '10kb' }));
//Enable the server to read cookies
app.use(cookieParser());
// Serve static files
// app.use(express.static(`${__dirname}/public`));
// app.use(express.static(`${__dirname}/client/build`)); - This works!
app.use(express.static(path.join(__dirname, 'client/build')));
// [ROUTES]
app.use('/api/listings', listingRouter);
app.use('/api/users', userRouter);
//Handling routes not defined
app.get('/*', function (req, res) {
res.sendFile(path.join(__dirname, 'client/build', 'index.html'));
});
app.all('*', (req, res, next) => {
next(new AppError(`${req.originalUrl} doesn't exist on this server!❌`), 404);
});
// Global Error Handling Middleware
app.use(globalErrorHandler);
module.exports = app;