node.js 请求页面保存在本地
1 var http = require('http'); 2 var fs = require('fs'); 3 4 var req = http.get({ host: "www.baidu.com" }); 5 6 fs.open('baidu.html', 'w', function (err, fd) { 7 if (err) { 8 console.error(err); 9 return; 10 } 11 var buffer; 12 req.on('response', function (res) { 13 14 res.setEncoding('utf8'); 15 res.on('data', function (data) { 16 buffer = new Buffer(data); 17 console.log(data); 18 // fs.writeFile('baidu.html', buffer); 19 20 fs.write(fd, buffer, 0, buffer.length); 21 }); 22 }); 23 24 });