Skip to content

Commit 9b195f8

Browse files
app:add logger
1 parent 1ac9ec0 commit 9b195f8

File tree

1 file changed

+17
-5
lines changed

1 file changed

+17
-5
lines changed

src/app.ts

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,41 @@
11
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 {
38
constructor(public port: number) {}
49

510
loggerMiddleware(
611
request: express.Request,
712
response: express.Response,
813
next: express.NextFunction
914
) {
10-
console.log(`${request.method} ${request.path}`);
15+
logger.info(`${request.method} ${request.path}`);
1116
next();
1217
}
1318

1419
createApp() {
1520
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+
);
1729
return app;
1830
}
1931

2032
startServer() {
2133
const app = this.createApp();
2234
app.listen(this.port, (err: any) => {
2335
if (err) {
24-
return console.error(err);
36+
return logger.error(err);
2537
}
26-
return console.log(`server is listening on ${this.port}`);
38+
return logger.info(`server is listening on ${this.port}`);
2739
});
2840
}
2941
}

0 commit comments

Comments
 (0)