Simple question I hope...

I want to use the following code from the Node documentation website for HTTPS (https://nodejs.org/api/https.html) but instead of stdout I want to write it to a file.

const https=require('https');https.get('https://encrypted.google.com/', (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);});

I found the following FS code for writing binary files but can't seem to successfully put the two together.

var crypto=require('crypto');var fs=require('fs');var wstream=fs.createWriteStream('myBinaryFile');// creates random Buffer of 100 bytesvar buffer=crypto.randomBytes(100);wstream.write(buffer);// create another Buffer of 100 bytes and writewstream.write(crypto.randomBytes(100));wstream.end();

Any ideas?

share|improve this question

    Try this:

    const https=require('https');const fs=require('fs');const wstream=fs.createWriteStream('myBinaryFile');https.get('https://encrypted.google.com/', (res)=> {console.log('statusCode:', res.statusCode);console.log('headers:', res.headers);res.on('data', (d)=> {wstream.write(d);});res.on('end', ()=> {wstream.end();})}).on('error', (e)=> {console.error(e);});
    share|improve this answer
    • 2
      Why not write in res.on('data', ...) ? That way you don't need to buffer the whole thing in memory.– user268396Feb 13 at 18:17
    • Thanx, Updated answer.– kgangadharFeb 13 at 18:22
    • Thanks. Looks so easy now you (both) have written it.– DazFeb 13 at 23:26

    Your Answer

     
    discard

    By posting your answer, you agree to the privacy policy and terms of service.

    Not the answer you're looking for? Browse other questions tagged or ask your own question.