2

I am trying to time how long it takes to respond to requests.

Right now I create a timestamp on the httpServer request event the problem is that httpServerResponse does not emit end and the end function to finish the response is called in different locations and I dont want to add this code at all these locations.

Is there a better way to do this?

var server = http.createServer(); server.on('request', dispatch); function dispatch(req, res){ var start = Date.now(); // No such event so never fired res.on('end', function(){ console.log('requests.time', Date.now() - start); }); generateResponse(req, res); } 

1 Answer 1

4

You could overwrite the original res.end() function and call the original from within it:

var server = http.createServer(); server.on('request', dispatch); function dispatch(req, res){ var start = Date.now(); var original=res.end; res.end=function(txt,encoding){ console.log('requests.time', Date.now() - start); original.call(this,txt,encoding); } generateResponse(req, res); } function generateResponse(req,res){ setTimeout(function(){ // simulate some processing res.end('hi'); },1000); } 
Sign up to request clarification or add additional context in comments.

1 Comment

yes I was thinking about doing that, I thought that there might be another way. thanks

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.