2
var data = import('./blahblah/data')
app.get('/data', data.getData)
app.listen(3000)

// blahblah/data:
exports.getData = function(req, res) {
    setTimeout(function(){
        res.send('test')
    }, 10000)
};

//code I use to test
for(var i = 0;i<10;i++){
    $.ajax({
      url: 'http://127.0.0.1:3000/data',
      success: function(l){console.log(l);}
    });
}

If I send 10 simultaneous requests to this endpoint, then they only receive a reply one at a time 10 seconds apart, with the last request coming 110 seconds after the first request was sent. Aren't nodejs and express supposed to let other requests process when the other requests are running asynchronous code?

10
  • 1
    How is getData used in your express app? Commented Feb 26, 2015 at 18:00
  • I updated the post to show how its used. Its just attached to a GET endpoint. Commented Feb 26, 2015 at 18:06
  • What makes you think it's not working async? Commented Feb 26, 2015 at 18:08
  • when I send 10 requests they return one at a time 10 seconds after the previous request. Commented Feb 26, 2015 at 18:09
  • 2
    Your browser might cause this. There are some limits on active connections per host. Commented Feb 26, 2015 at 18:11

1 Answer 1

6

Your code is fine, probably something is wrong with the way you're testing it.

Please, make sure that you're sending your requests in parallel.

I tested your code with loadtest utility

loadtest -n 10 -c 10 http://localhost:3000/data

and got expected results

Target URL:          http://localhost:3000/data
Max requests:        10
Concurrency level:   10
Agent:               none

Completed requests:  10
Total errors:        0
Total time:          10.050681130000001 s

Here is a code snipped I tested:

var app = require('express')();
app.get('/data', function(req, res) {
    setTimeout(function(){
        res.send('test')
    },10000)
});
app.listen(3000)

Update:

Web browsers usually limit the number of parallel requests per host. I just tested it in my Google Chrome and it limited the number of parallel requests by 6:

enter image description here

Here is a detailed timing for one of delayed requests:

enter image description here

Looks like your browser have an even harsher limits.

Sign up to request clarification or add additional context in comments.

3 Comments

I added the way I test to the post, can you see whats wrong with that?
That detail view with the "stalled" time is the perfect illustration!
yeah it was being stalled. For some reason my browser has some strict limits. Thanks for your help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.