A
A
Arseny Sokolov2015-03-28 18:21:12
Google
Arseny Sokolov, 2015-03-28 18:21:12

How to wait for http response to finish loading in Node.js?

I'm making a site that has oAuth authorization/registration. Stuck on the problem of authorization through Google. I absolutely do not want to use googleapis modules for Node.js, because for each provider I write the authorization and registration algorithm itself.
The problem is that when I request an access_token , google displays the answer in parts, 3 parts in total, which I don't know how to put together.
I use to get a response:

res.on('data', function(chunk) {
  gp_res = JSON.parse(chunk);
});

In this case, an error occurs due to the fact that the chunk becomes empty at the end.
If you output for debugging via console.log(chunk) , you can see how the server response is divided into 3 parts.
If you output data using process.stdout.write(chunk) , then somehow the data is collected in one data block.
How can I collect all the data? I met this for the first time, with the rest of Facebook, Odnoklassniki, Vkontakte, this was not the case.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry Demin, 2015-03-28
@ArsenBespalov

https://nodejs.org/api/http.html#http_class_http_c...
Read the documentation and guides. Fortunately, according to the standard API functions, you can find them through the same Google.
As for your example, write data somewhere within the scope of your closure.

var _json='';

socket.on('data', function(chunk) {
    _json+=chunk.toString();
});

socket.on('end', function() {
    var result=JSON.parse(_json);
    ...
});

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question