Created
December 16, 2013 05:56
-
-
Save vitrum/7982896 to your computer and use it in GitHub Desktop.
require html page by nodejs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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