Created
January 5, 2018 05:14
-
-
Save DerekHung/f8f6019a1601ead0cfba4152f4878bc2 to your computer and use it in GitHub Desktop.
middleware performance checker
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { | |
EventEmitter | |
} from 'events'; | |
let instance = null; | |
class MdwLogger { | |
constructor() { | |
if (!instance) { | |
instance = this; | |
instance.event = new EventEmitter(); | |
instance.event.on('middleware', ({ | |
req, | |
name | |
}) => { | |
console.timeEnd(`${req.method} ${req.url} : ${name}`); | |
}); | |
} | |
return instance; | |
} | |
wrap(fn, name) { | |
const that = this; | |
return function (req, res, next) { | |
const fnName = fn.name || name; | |
console.time(`${req.method} ${req.url} : ${fnName}`); | |
fn(req, res, function () { | |
that.event.emit('middleware', { | |
req, | |
name: fn.name || name | |
}); | |
next.apply(this, arguments); | |
}); | |
}; | |
} | |
} | |
export default MdwLogger; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage: