-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
64 lines (56 loc) · 1.71 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
const express = require('express');
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 morgan = require('morgan');
const compress = require('compression');
const AppError = require('./utils/appError');
const httpCodes = require('./utils/httpStatuses');
const globalErrorHandler = require('./controllers/errorController');
const blogRoutes = require('./routes/blogRoutes');
const authRoutes = require('./routes/authRoutes');
const userRoutes = require('./routes/userRoutes');
const tagRoutes = require('./routes/tagRoutes');
const app = express();
app.use(helmet());
// if we are in dev use morgan
if (process.env.NODE_ENV === 'development') {
app.use(morgan('dev'));
}
// Use Rate Limiter
const limiter = rateLimit({
max: 100,
windowMs: 60 * 60 * 1000,
message: 'Too many requests from this IP, try again in an hour',
});
// Middleware
app.use('/api', limiter);
app.use(express.json());
// Security Middleware
app.use(mongoSanitize());
app.use(xss());
app.use(hpp());
// Compression
app.use(compress());
// Routes
const API_ROOT_ENDPOINT = '/api/v1';
app.use(`${API_ROOT_ENDPOINT}/blogs`, blogRoutes);
app.use(`${API_ROOT_ENDPOINT}/auth`, authRoutes);
app.use(`${API_ROOT_ENDPOINT}/users`, userRoutes);
app.use(`${API_ROOT_ENDPOINT}/tags`, tagRoutes);
// Static Files
app.use(express.static('public'));
// Handling unhandled routes
app.all('*', (req, res, next) => {
next(
new AppError(
`Cant find ${req.originalUrl} on this server!`,
httpCodes.HTTP_NOT_FOUND
)
);
});
// Bind the global error handler
app.use(globalErrorHandler);
module.exports = app;