UNPKG

881 BJavaScriptView Raw
1const { log, timeEnd } = require('./../namespace/console');
2const { color } = new (require('./../namespace/string'))();
3
4module.exports = class RequestEnd {
5
6 constructor (project, onAfterResponse) {
7 this.logging = require(project.configPath).logHTTP;
8 this.onAfterResponse = onAfterResponse;
9 }
10
11 end (req, res, next) {
12 req.on("end", () => {
13
14 if (this.logging) {
15 let code = res.statusCode;
16 let _color = (code >= 400 && code < 500) ? "yellow" : (code >= 500) ? "red" : "green";
17 log(
18 color(`Response ${req.id} >> ${res.statusCode} ${res.statusMessage} [${(res._headers && res._headers['content-length'] || 0)} bytes]`, _color)
19 );
20 timeEnd(`Timing ${req.id}`);
21 }
22
23 this.mapEndingHook(req, res);
24 });
25 next();
26 }
27
28
29 mapEndingHook (req, res) {
30 // ... Implement the onAfterEnd Hooks
31 }
32
33}