Skip to content

Instantly share code, notes, and snippets.

@vitrum
Created December 16, 2013 05:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vitrum/7982896 to your computer and use it in GitHub Desktop.
Save vitrum/7982896 to your computer and use it in GitHub Desktop.
require html page by nodejs
var http = require('http');
var fs = require('fs');
   
var req = http.request({
host: 'www.bbc.com'
}, function(res) {   
var arr = [],
len = 0,
totalLen=0;
res.on('data', function(data) {
console.log(data);
len++;
arr.push(data);
totalLen += data.length;
});
res.on('end', function() {
var buffer = new Buffer(totalLen);
for(var i = 0, pos = 0; i < len; i++) {
var chunk = arr[i];
chunk.copy(buffer, pos);
pos += chunk.length;
}
fs.writeFile('file.txt',buffer);
});
});
req.end();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment