how to create response time router node js
const app = require('express')();
const bodyParser = require('body-parser');
const { EventEmitter } = require('events');
// Aggregate all profiler results into an event emitter to make
// handling the results generic
const profiles = new EventEmitter();
profiles.on('middleware', ({ req, name, elapsedMS }) => {
console.log(req.method, req.url, ':', name, `${elapsedMS}ms`);
});
app.use(wrap(function block(req, res, next) {
setTimeout(() => next(), 1000);
}));
app.use(wrap(bodyParser.json()));
app.post('*', function(req, res) {
res.send('Hello, world!');
});
app.listen(3000);
function wrap(fn) {
return function(req, res, next) {
const start = Date.now();
fn(req, res, function() {
profiles.emit('middleware', {
req,
name: fn.name,
elapsedMS: Date.now() - start
});
next.apply(this, arguments);
});
};
}module.exports = function responseTime(){
return function(req, res, next){
var start = new Date;
if (res._responseTime) return next();
res._responseTime = true;
res.on('header', function(){
var duration = new Date - start;
res.setHeader('X-Response-Time', duration + 'ms');
});
next();
};
};
Also in JavaScript:
- js console log input value
- sorting number with coma datatable
- how to get file extension in javascript last index
- npm create react app
- target data option select vue js
- how to change package name in react native
- send message to specific channel discord.js
- Function to convert an Array to an Associative array
- parsing through json without using key value python
- javascript sessionStorage
- ERROR in ./node_modules/tns-core-modules/ui/core/view/view.js Module not found: Error: Can't resolve '@nativescript/core/ui/core/view/view' in '/home/deepali/projects/supersquad-client/node_modules/tns-core-modules/ui/core/view'
- javascript turn string into array
- hot to start cypress
- python json.dumps pretty print
- javascript extract date from string
- json.stringify stack overflow
- JavaScript banner
- javascript console input
- python run javascript
- jquery get each row in table
- split a message js
- how to change css variable in javascript
- notification dropdown bootstrap
- install node modules