-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
49 lines (38 loc) · 1.21 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
const express = require('express');
const morgan = require('morgan');
const compression = require('compression');
const cookieParser = require('cookie-parser');
const helmet = require('helmet');
const AppError = require('./utils/appError');
const globalErrorHandler = require('./controllers/errorController');
const houseRouter = require('./routes/houseRoutes');
const userRouter = require('./routes/userRoutes');
const reviewRouter = require('./routes/reviewRoutes');
const cors = require('cors');
const app = express();
// 1) MIDDLEWARES
if (process.env.NODE_ENV === 'development') {
app.use(morgan('dev')); //to log req/res info
}
//Set security HTTP headers
app.use(helmet());
//Implement CORS
app.use(cors());
app.options('*', cors());
//Body parser
app.use(express.json({ limit: '10kb' }));
app.use(cookieParser());
app.use(compression());
app.use((req, res, next) => {
req.requestTime = new Date().toISOString();
next();
});
// 2) ROUTES
app.use('/api/houses', houseRouter);
app.use('/api/users', userRouter);
app.use('/api/reviews', reviewRouter);
app.all('*', (req, res, next) => {
next(new AppError(`Can't find ${req.originalUrl} on this server!`, 404));
});
app.use(globalErrorHandler);
module.exports = app;