Mr Null Pointer
Mr Null Pointer

Reputation: 111

HTTPS module in nodeJS get method

I am planning to parse JSON after making a get request with the node HTTPS module. I saw the documentation but was unable to understand the part highlighted below. I want to know more about res.on('data', (d) => { process.stdout.write(d); }) method. How it fetches the data? const https = require('https');

'''

https.get('someUrl', (res) => {
  console.log('statusCode:', res.statusCode);
  console.log('headers:', res.headers);

  *res.on('data', (d) => {
    process.stdout.write(d);
  });*

}).on('error', (e) => {
  console.error(e);
});

'''

Upvotes: 1

Views: 352

Answers (1)

T.Trassoudaine
T.Trassoudaine

Reputation: 1375

To add some information about rags2riches comment, the function associated to res.on('url', function) will be called for every chunk of data received and d will basically contain the data of this chunk. If you want to do something when you have received everything, you should use res.on('close', function) or res.on('end', function). An example here: https://blog.bearer.sh/node-http-request/

Upvotes: 1

Related Questions