3

Is there any way that I can make express server handle several GET requests in parallel?

Consider this example:

server.js

var express = require('express')
var app = express();

app.get('/test01', function (req, res, next) {
    for (i = 0; i < 1000000000; i++) {
        var temp = i*i;
    }
    res.end('{"success" : "test01", "status" : 200}');
});

app.get('/test02', function (req, res, next) {
    for (i = 0; i < 1; i++) {
        var temp = i*i;
    }
    res.end('{"success" : "test02", "status" : 200}');
});

app.listen(8081, function () {
    console.log("server listening at  %s:%s", this.address().address, this.address().port)
})

client.js

var request = require("request");

request("http://127.0.0.1:8081/test01", function(error, response, body) {
    console.log(body);
});
request("http://127.0.0.1:8081/test02", function(error, response, body) {
    console.log(body);
});

After running the client.js, the output is:

{"success" : "test01", "status" : 200}
{"success" : "test02", "status" : 200}

which means that the second request will have to wait for the first request to be finished.

What do I need to change in server.js in order to make both of the requests run in parallel and making "test01" non blocking service and finishing test02 before test01?

I don't want to change anything in client.js and I can't control the timing of the calling of the services. e.g test01 may be call 3 times sequentially and than test02 may be called.

Idan Yehuda
  • 524
  • 7
  • 21
  • 2
    https://codeforgeek.com/2014/12/cluster-node-js-performance/ – Wiktor Zychla Mar 04 '16 at 21:34
  • 1
    Possible duplicate of [(Solution Found) Node.js Async parallel requests are running sequentially](http://stackoverflow.com/questions/32442426/solution-found-node-js-async-parallel-requests-are-running-sequentially) – cmorrissey Mar 04 '16 at 21:36
  • My case is a different, I can't control the timing of each service call. – Idan Yehuda Mar 04 '16 at 23:10
  • If this would work like you describe, the whole Node.js platform would be pretty useless. I cannot find any reason why the responses should come in that order. Have you actually tried to run this exact code? – Robert Rossmann Mar 04 '16 at 23:39
  • yep, I created this small example and run it in order to be more clear about my problem. – Idan Yehuda Mar 04 '16 at 23:42
  • I copied your code and run it on node v4.2.6 and I got test2 before test1. Maybe issue is not related with this code? – just-boris Mar 05 '16 at 23:36
  • Please Ignore the use of setTimeout, I have edited server.js to my original question and test it again and it showed test01 before test02. It seem like the only way to tackle it is by using 'cluster' module. – Idan Yehuda Mar 06 '16 at 00:08

1 Answers1

3

Node js is a single process running on your machine, i.e. a single instance of Node runs in a single thread. So if you are doing something computation heavy, node is probably not the right choice, that being said, the real power of node is in async & callbacks. You could try using 'cluster' node module, the idea is rather than having one thread running on one cpu unit, you run as many as possible based on # of cpu's, so for eg: on quad core you can run upto 4 node instances. Hope this helps.

Karthik
  • 1,386
  • 12
  • 7