/ hapijs

Introducing hapi-response-time

hapi-response-time
In the last post, we discussed about the quick way of creating REST APIs with HapiJS; I am very much delighted to introduce the hapi-response-time plugin.This plugin will help in measuring the time taken by any api endpoint to generate the response and can be handy from the debugging point of view.

To set it up, you would need to install it as dependency and register the plugin in following way:

Adding Dependency

npm i -S hapi-response-time

Installation

server.register(require('hapi-response-time'), err => { 
    if(err) { 
        throw err;
    }
});

And now all the requests will have the following headers:

x-req-start → 1484305451729
x-res-end → 1484305451738
x-response-time → 9

The plugin is available on github to see into: https://github.com/pankajpatel/hapi-response-time

To test it out, you can use following code:

var Hapi = require('hapi');
var server = new Hapi.Server();
 
server.connection({
  port: process.env.PORT || 3000
});
server.register(require('hapi-response-time'), (err) => {
  if(err) {
    throw err;
  }
})
 
server.route([
  {
    method: 'GET',
    path: '/john',
    handler: function(req, reply) {
      reply('Hello John!');
    }
  },{
    method: 'GET',
    path: '/timeout',
    handler: function (request, reply) {
      setTimeout(function() {
        reply('Response after 10 seconds')
      }, 10000);
    }
  }
]);

So use it in your hapijs based projects and let us know about your feedback in comments or github issue and if you like it or find it useful, please star it on github.