0

When I make a http request, I need to concatenate the response:

request.on('response', function (response) {
var body = '';
response.on('data', function (chunk) {
    body += chunk;
    });
...

Why was that implemented this way? Why not output the whole result?

5
  • 1
    What if the whole result takes up more memory than you have on your system? Commented May 31, 2015 at 18:01
  • Use express js it may be more suitable for you : express. You will find it easier to develop NodeJS (it is a bit its purpose in fact). Commented May 31, 2015 at 18:02
  • By doing this you're not concatenating the response, but you are concatenating the data in the response as a buffer. Commented May 31, 2015 at 18:02
  • @MacKentoch thanks, I already know express, just wanted to understand why should I do it in first place. Commented May 31, 2015 at 18:03
  • @ExplosionPills, good point, but AJAX get() return the whole result and I never saw anyone complaining. Commented May 31, 2015 at 18:04

2 Answers 2

1

What you're getting back is a stream, which is a very handy construct in node.js. Required reading: https://github.com/substack/stream-handbook

If you want to wait until you've received the whole response, you can do this very easily:

var concat = require('concat-stream');

request.on('response', function(response) {
    response.pipe(concat(function(body) {
        console.log(body);
    }));
});
Sign up to request clarification or add additional context in comments.

Comments

1

Node only uses a single process, no thread. This mean that if spend a lot of time doing something you can´t process other things, like for example other client requests...

For that reason when you are coding in node, you need code thinking in async way.

In this scenario, the request could be slowly, and the program will wait for this request doing nothing.

I found this: Why is node.js asynchronous?

And this is so interesting as well: http://en.wikipedia.org/wiki/Read%E2%80%93eval%E2%80%93print_loop

Comments

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.