improvement - logger middleware
MODIFY
package-lock.json
package-lock.json
"js-tokens": "3.0.2"
}
},
+ "@types/chalk": {
+ "version": "2.2.0",
+ "resolved": "https://registry.npmjs.org/@types/chalk/-/chalk-2.2.0.tgz",
+ "integrity": "sha512-1zzPV9FDe1I/WHhRkf9SNgqtRJWZqrBWgu7JGveuHmmyR9CnAPCie2N/x+iHrgnpYBIcCJWHBoMRv2TRWktsvw==",
+ "requires": {
+ "chalk": "2.4.1"
+ }
+ },
"@types/events": {
"version": "1.2.0",
"resolved": "https://registry.npmjs.org/@types/events/-/events-1.2.0.tgz",
"version": "3.2.1",
"resolved": "https://registry.npmjs.org/ansi-styles/-/ansi-styles-3.2.1.tgz",
"integrity": "sha512-VT0ZI6kZRdTh8YyJw3SMbYm/u+NqfsAxEpWO0Pf9sq8/e94WxxOpPKx9FR1FlyCtOVDNOQ+8ntlqFxiRc+r5qA==",
- "dev": true,
"requires": {
"color-convert": "1.9.1"
}
"version": "2.4.1",
"resolved": "https://registry.npmjs.org/chalk/-/chalk-2.4.1.tgz",
"integrity": "sha512-ObN6h1v2fTJSmUXoS3nMQ92LbDK9be4TV+6G+omQlGJFdcUX5heKi1LZ1YnRMIgwTLEj3E24bT6tYni50rlCfQ==",
- "dev": true,
"requires": {
"ansi-styles": "3.2.1",
"escape-string-regexp": "1.0.5",
"version": "1.9.1",
"resolved": "https://registry.npmjs.org/color-convert/-/color-convert-1.9.1.tgz",
"integrity": "sha512-mjGanIiwQJskCC18rPR6OmrZ6fm2Lc7PeGFYwCmy5J34wC6F1PzdGL6xeMfmgicfYcNLGuVFA3WzXtIDCQSZxQ==",
- "dev": true,
"requires": {
"color-name": "1.1.3"
}
"color-name": {
"version": "1.1.3",
"resolved": "https://registry.npmjs.org/color-name/-/color-name-1.1.3.tgz",
- "integrity": "sha1-p9BVi9icQveV3UIyj3QIMcpTvCU=",
- "dev": true
+ "integrity": "sha1-p9BVi9icQveV3UIyj3QIMcpTvCU="
},
"combined-stream": {
"version": "1.0.6",
"escape-string-regexp": {
"version": "1.0.5",
"resolved": "https://registry.npmjs.org/escape-string-regexp/-/escape-string-regexp-1.0.5.tgz",
- "integrity": "sha1-G2HAViGQqN/2rjuyzwIAyhMLhtQ=",
- "dev": true
+ "integrity": "sha1-G2HAViGQqN/2rjuyzwIAyhMLhtQ="
},
"escodegen": {
"version": "1.9.1",
"has-flag": {
"version": "3.0.0",
"resolved": "https://registry.npmjs.org/has-flag/-/has-flag-3.0.0.tgz",
- "integrity": "sha1-tdRU3CGZriJWmfNGfloH87lVuv0=",
- "dev": true
+ "integrity": "sha1-tdRU3CGZriJWmfNGfloH87lVuv0="
},
"has-value": {
"version": "1.0.0",
"version": "5.4.0",
"resolved": "https://registry.npmjs.org/supports-color/-/supports-color-5.4.0.tgz",
"integrity": "sha512-zjaXglF5nnWpsq470jSv6P9DwPvgLkuapYmfDm3JWOm0vkNTVF2tI4UrN2r6jH1qM/uc/WtxYY1hYoA2dOKj5w==",
- "dev": true,
"requires": {
"has-flag": "3.0.0"
}

MODIFY
package.json
package.json
},
"homepage": "https://github.com/JozefFlakus/node-rx#readme",
"dependencies": {
+ "@types/chalk": "^2.2.0",
+ "chalk": "^2.4.1",
"rxjs": "~6.0.0"
},
"devDependencies": {

MODIFY
src/middlewares/logger.middleware.ts
src/middlewares/logger.middleware.ts
-import { Http } from '../http.interface';
+import chalk from 'chalk';
+import { Http, HttpResponse } from '../http.interface';
+import { Middleware } from './middleware.interface';
+
+const getStatusCode = (statusCode: number): string =>
+ statusCode >= 400
+ ? chalk.red(statusCode.toString())
+ : chalk.green(statusCode.toString());
+
+export const loggerMiddleware: Middleware = http => {
+ const { info } = console;
+ const { req, res } = http;
+
+ const hrStartTime = process.hrtime();
+
+ res.on('finish', () => {
+ const endTime = process.hrtime(hrStartTime)[1] / 1000000;
+ info(
+ `%s %s %s %dms`,
+ req.method,
+ req.url,
+ getStatusCode(res.statusCode),
+ Math.round(endTime * 100) / 100
+ );
+ });
-export const loggerMiddleware = (http: Http) => {
- console.log('Logger:', `${http.req.url}`);
return http;
};

ADD
src/middlewares/middleware.interface.ts
+import { Http } from '../http.interface';
+
+export type Middleware = (http: Http) => Http;

improvement - logger middleware