更新
去年接受的答案很好,但今天我会使用其他人使用的包: https ://github.com/mikeal/request
原来的
我正在尝试获取 google 的徽标并使用 node.js 将其保存到我的服务器。
这就是我现在拥有的并且不起作用:
var options = {
host: 'google.com',
port: 80,
path: '/images/logos/ps_logo2.png'
};
var request = http.get(options);
request.on('response', function (res) {
res.on('data', function (chunk) {
fs.writeFile(dir+'image.png', chunk, function (err) {
if (err) throw err;
console.log('It\'s saved!');
});
});
});
我怎样才能得到这个工作?