Reputation: 82
I am trying to write a simple API manager in Node using express.
I have registered a simple route and I responded after 10 seconds to every alternate requests. I suppose I should get immediate response from every other requests which I am not getting. Even requests gets hanged as it should be, but odd requests should give me response immediately which I am not receiving. Kindly guide how can to do that.
var express = require('express');
var apiRoutes = express.Router();
var i=0;
apiRoutes.get('/',function(req,res){
i++;
if(i%2==0)
{
setTimeout(function(){res.json({message:"i responsed after 5"})},5000);
}
else
{
res.json({message:"i responsed immediately"});
}
});
module.exports = apiRoutes;
Upvotes: 0
Views: 1125
Reputation: 1785
After some searching, I've found this answer. So it appears it's the browser, and not the express code.
To test this, hit the server from your terminal (curl localhost:3000
) and you can see it behave as you expect!
Alternatively, use this line apiRoutes.get('/:number', function(req,res){
, then from your browser hit localhost:3000/1
and localhost:3000/2
and you'll see the behavior you expect as well.
Upvotes: 1