-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.ts
51 lines (38 loc) · 1.28 KB
/
app.ts
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
import express from 'express';
import { Request, Response, NextFunction, Application } from 'express';
import morgan from 'morgan';
import helmet from 'helmet';
import xss from 'xss-clean';
import cookieParser from 'cookie-parser';
// const compression = require('compression');
import { AppError } from './utils/appError';
import globalErrorHandler from './controllers/errorController';
import bookRouter from './routes/bookRoutes';
const app: Application = express();
// Global MiddleWares
// Security HTTP Headers
app.use(helmet({ contentSecurityPolicy: false }));
// Development logging
if (process.env.NODE_ENV === 'development') {
app.use(morgan('dev'));
}
// Body Parser
app.use(express.json({ limit: '10kb' }));
app.use(express.urlencoded({ extended: true, limit: '10kb' }));
app.use(cookieParser());
// Data sanitization against XSS
app.use(xss());
// compress responses
// app.use(compression());
// Test middleware
app.use((req: Request, res: Response, next: NextFunction) => {
req.requestTime = new Date().toISOString();
next();
});
// Routes
app.use('/api/books', bookRouter);
app.all('*', (req: Request, res: Response, next: NextFunction) => {
next(new AppError(`Can't find ${req.originalUrl} on this Server!`, 404));
});
app.use(globalErrorHandler);
export default app;