Skip to content

Instantly share code, notes, and snippets.

@momenbasel
Created June 10, 2017 08:33
Show Gist options
  • Save momenbasel/c8e3ff19a31bea3f500d81e9856bb5ba to your computer and use it in GitHub Desktop.
Save momenbasel/c8e3ff19a31bea3f500d81e9856bb5ba to your computer and use it in GitHub Desktop.
Send https to wiki and create HTML file with node js.
var https = require('https');
var fs = require('fs');
var options = {
hostname : "en.wikipedia.org",
port : 443,
path : "/wiki/Google",
method : "GET"
};
var req = https.request(options, function(res){
var responseBody = "";
console.log('Response started!');
console.log(`Server status: ${res.statusCode}`);
console.log("Response header %j", res.headers);
res.setEncoding('UTF-8');
res.once('data', function(chunck){
console.log(chunck);
});
res.on('data', function(chunck){
console.log(`---chunck --- ${chunck.length}`);
responseBody+= chunck;
});
res.on("end", function(){
fs.writeFile('google-wikipedia.html', responseBody, function(err){
if(err){
throw err;
}
console.log('file downloaded successfully');
});
})
});
// if there are error on the request
req.on('error', function(err){
console.log(`problem with request : ${err.message}`);
});
req.end();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment