|
1 | | -import errorHandler from 'errorhandler'; |
2 | | -import app from './app'; |
3 | 1 | import container from './config/dependency-injection'; |
| 2 | +import errorHandler from 'errorhandler'; |
| 3 | +import helmet from 'helmet'; |
| 4 | +import compress from 'compression'; |
| 5 | +import bodyParser from 'body-parser'; |
| 6 | +import express from 'express'; |
| 7 | +import * as http from 'http'; |
| 8 | +import Logger from '../../../Contexts/Shared/domain/Logger'; |
| 9 | +import { registerRoutes } from './routes'; |
4 | 10 |
|
5 | | -/** |
6 | | - * Error Handler. Provides full stack - remove for production |
7 | | - */ |
8 | | -app.use(errorHandler()); |
| 11 | +export class Server { |
| 12 | + private express: express.Express; |
| 13 | + private port: string; |
| 14 | + private logger: Logger; |
| 15 | + private httpServer?: http.Server; |
9 | 16 |
|
10 | | -/** |
11 | | - * Start Express server. |
12 | | - */ |
13 | | -const server = app.listen(app.get('port'), () => { |
14 | | - // tslint:disable: no-console |
15 | | - const logger = container.get('Shared.Logger'); |
| 17 | + constructor(port: string) { |
| 18 | + this.port = port; |
| 19 | + this.logger = container.get('Shared.Logger'); |
| 20 | + this.express = express(); |
| 21 | + this.express.use(bodyParser.json()); |
| 22 | + this.express.use(bodyParser.urlencoded({ extended: true })); |
| 23 | + this.express.use(helmet.xssFilter()); |
| 24 | + this.express.use(helmet.noSniff()); |
| 25 | + this.express.use(helmet.hidePoweredBy()); |
| 26 | + this.express.use(helmet.frameguard({ action: 'deny' })); |
| 27 | + this.express.use(compress()); |
| 28 | + registerRoutes(this.express); |
| 29 | + this.express.use(errorHandler()); |
| 30 | + } |
16 | 31 |
|
17 | | - logger.info(` App is running at http://localhost:${app.get('port')} in ${app.get('env')} mode`); |
18 | | - console.log(' Press CTRL-C to stop\n'); |
19 | | -}); |
| 32 | + async listen(): Promise<void> { |
| 33 | + return new Promise(resolve => { |
| 34 | + this.httpServer = this.express.listen(this.port, () => { |
| 35 | + this.logger.info(` App is running at http://localhost:${this.port} in ${this.express.get('env')} mode`); |
| 36 | + this.logger.info(' Press CTRL-C to stop\n'); |
| 37 | + resolve(); |
| 38 | + }); |
| 39 | + }); |
| 40 | + } |
20 | 41 |
|
21 | | -export default server; |
| 42 | + stop() { |
| 43 | + if (this.httpServer) { |
| 44 | + this.httpServer.close(); |
| 45 | + } |
| 46 | + } |
| 47 | +} |
0 commit comments