|
1 | 1 | import express from 'express'; |
2 | | -export class App { |
| 2 | +import compression from 'compression'; // compresses requests |
| 3 | +import bodyParser from 'body-parser'; |
| 4 | +import lusca from 'lusca'; |
| 5 | +import path from 'path'; |
| 6 | +import logger from './util/logger'; |
| 7 | +export default class App { |
3 | 8 | constructor(public port: number) {} |
4 | 9 |
|
5 | 10 | loggerMiddleware( |
6 | 11 | request: express.Request, |
7 | 12 | response: express.Response, |
8 | 13 | next: express.NextFunction |
9 | 14 | ) { |
10 | | - console.log(`${request.method} ${request.path}`); |
| 15 | + logger.info(`${request.method} ${request.path}`); |
11 | 16 | next(); |
12 | 17 | } |
13 | 18 |
|
14 | 19 | createApp() { |
15 | 20 | const app: express.Application = express(); |
16 | | - app.use(this.loggerMiddleware); |
| 21 | + app.use(compression()); |
| 22 | + app.use(bodyParser.json()); |
| 23 | + app.use(bodyParser.urlencoded({ extended: true })); |
| 24 | + app.use(lusca.xframe('SAMEORIGIN')); |
| 25 | + app.use(lusca.xssProtection(true)); |
| 26 | + app.use( |
| 27 | + express.static(path.join(__dirname, 'public'), { maxAge: 31557600000 }) |
| 28 | + ); |
17 | 29 | return app; |
18 | 30 | } |
19 | 31 |
|
20 | 32 | startServer() { |
21 | 33 | const app = this.createApp(); |
22 | 34 | app.listen(this.port, (err: any) => { |
23 | 35 | if (err) { |
24 | | - return console.error(err); |
| 36 | + return logger.error(err); |
25 | 37 | } |
26 | | - return console.log(`server is listening on ${this.port}`); |
| 38 | + return logger.info(`server is listening on ${this.port}`); |
27 | 39 | }); |
28 | 40 | } |
29 | 41 | } |
0 commit comments