-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
58 lines (43 loc) · 1.22 KB
/
app.js
File metadata and controls
58 lines (43 loc) · 1.22 KB
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
const express = require('express');
const morgan = require('morgan');
const cors = require('cors');
const helmet = require('helmet');
const xss = require('xss-clean');
const rateLimit = require('express-rate-limit');
const AppError = require('./utils/appError');
const globalErrorHandler = require('./controllers/errorController');
const bookRouter = require('./routes/bookRoutes');
const app = express();
const limiter = rateLimit({
max: 100,
windowMs: 60 * 60 * 1000,
message: 'Too many request from this IP. Please try again in a hour.',
});
app.enable('trust proxy');
// Globala Middleware
app.use(
cors({
origin: 'http://localhost:8001',
credentials: true,
})
);
if (process.env.NODE_ENV === 'development') {
app.use(morgan('dev'));
}
app.use(helmet());
app.use('/api', limiter);
app.use(express.json({ limit: '5mb' }));
app.use(express.urlencoded({ extended: true, limit: '5mb' }));
// Data sanitering mot XSS.
app.use(xss());
app.use((req, res, next) => {
req.requestTime = new Date().toISOString();
next();
});
// Routes
app.use('/api/book', bookRouter);
app.all('*', (req, res, next) => {
next(new AppError(`Can't find ${req.originalUrl}!`, 404));
});
app.use(globalErrorHandler);
module.exports = app;