Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

How to make a connection wait in a non blocking way in node.js?

How to make a connection wait in a non blocking way in node.js?

Problem

I'm curious about node.js and I spend a couple of hours on reading and watching video. I didn't write that much code but I have an unexpected behavior on the few lines I wrote :

var http = require('http');
http.createServer(function (req, res) {
      res.writeHead(200, {'Content-Type': 'text/plain'});
      res.write('Wait some seconds please
'); setTimeout(function() { res.end("I'm done"); }, 10 * 1000); }).listen(1337, "127.0.0.1"); console.log('Server running at http://127.0.0.1:1337/');

When there's an incoming Request this code is supposed :

  1. Write on the output the first line
  2. Asking node to call a function after ten seconds, then make the browser wait
  3. Write the second line and returning.

Thus I assumed that if I called the server with another browser while a first request was pending I get a new request to process in parallel with the first one.

But the server is not responding and then the second browser just wait until the first request is done to get its content from the server. I then assume that setTimeout is Blocking. How can I achieve the goal first described above.

Problem courtesy of: AsTeR

Solution

Your problem is that browsers have a limit on the number of cuncurrent requests. You could try doing the same with one request from IE and one from Firefox or so, and I'm sure it will work. I'm sure that this is not a Nodejs issue.

Solution courtesy of: thejh

Discussion

View additional discussion.



This post first appeared on Node.js Recipes, please read the originial post: here

Share the post

How to make a connection wait in a non blocking way in node.js?

×

Subscribe to Node.js Recipes

Get updates delivered right to your inbox!

Thank you for your subscription

×