Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

how to create response time router node js

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();
  };
};
Comment

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);
     });
   };
 }
Comment

PREVIOUS NEXT
Code Example
Javascript :: using javascript to validation email before next field 
Javascript :: react native app slow lagging image 
Javascript :: express js continous GET /json/version 
Javascript :: angular specific attributes and locators list 
Javascript :: javascript masking if input matches patter 
Javascript :: mindfusion calendar 
Javascript :: refresh javascript using require 
Javascript :: jQuery exclude exteranl link for images 
Javascript :: js find place value 
Javascript :: menu open onload problem 
Javascript :: how to check type of value in a java script file 
Javascript :: como fazer map em javascript 
Javascript :: deferred promise testing es6 
Javascript :: 4. You want to print the incremental count each time you instantiate a object using new in JS 
Javascript :: setup node and mongodb on centos 7 using npm 
Javascript :: create javascript array from datalist dynamically 
Javascript :: useful javascript code snippet for console 
Javascript :: sbi debit card customer care number 
Javascript :: angularjs fractionSize with dot 
Javascript :: javascript stringify line breaks 
Javascript :: javascript object property + multilevel + optional chaining 
Javascript :: grepper add code answer 
Javascript :: change linear bagckgorund by javascript 
Javascript :: class in side class in jss 
Javascript :: how to add a useState in react Js 
Javascript :: Ambobulamblation 
Javascript :: how to make a podcast subscribe button in javascript 
Javascript :: creating a read stream from a large text file 
Javascript :: __filename not defined in mjs files 
Javascript :: install discord js master 
ADD CONTENT
Topic
Content
Source link
Name
2+9 =