I'm trying to download a file using node.js using the following script:
var http = require('http');
var fs = require('fs');
var file = fs.createWriteStream("google.html");
var request = http.get("http://www.google.com/", function(response) {
response.pipe(file);
});
and I keep getting the following error on Windows 7:
node.js:201
throw e; // process.nextTick error, or 'error' event on first tick
^
Error: connect EADDRNOTAVAIL
at errnoException (net.js:670:11)
at Object.afterConnect [as oncomplete] (net.js:661:19)
Press any key to continue . . .
...and the following error on Linux Mint 13:
node.js:201
throw e; // process.nextTick error, or 'error' event on first tick
^
Error: connect ECONNREFUSED
at errnoException (net.js:646:11)
at Object.afterConnect [as oncomplete] (net.js:637:18)
What is the most likely cause of this error, and how can I resolve it? The url isn't blocked on my network, so I'm not sure why this isn't working.
Try this.
var http = require('http');
var options = {
hostname: 'www.google.com',
port: 80,
path: '/index.html',
method: 'GET'
};
var req = http.request(options, function(res) {
console.log('STATUS: ' + res.statusCode);
console.log('HEADERS: ' + JSON.stringify(res.headers));
res.setEncoding('utf8');
res.on('data', function (chunk) {
console.log('BODY: ' + chunk);
});
});
req.on('error', function(e) {
console.log('problem with request: ' + e.message);
});
req.end();