简体   繁体   中英

node.js http.get() throws: events.js:72 ECONNREFUSED

I'm new to JavaScript and Node.js and trying to solve the challenge "juggling async" of learnyounode. When the error occured, I've tested my other solutions that use http.get() and all of them throw the exact same error. They worked just fine before, though.

That's a code that worked before:

var http = require('http');

var url = process.argv[2];

function cb_resp(res) {
   res.setEncoding('utf-8');
   res.on("data", console.log);
   res.on("error", console.error);
}

var respo = http.get(url, cb_resp)

The error I keep getting:

$node httpclient.js www.google.de

events.js:72
        throw er; // Unhandled 'error' event
              ^
Error: connect ECONNREFUSED
    at errnoException (net.js:904:11)
    at Object.afterConnect [as oncomplete] (net.js:895:19)

If it helps, I run a Mac with OS X 10.10.1 and use Brackets as my IDE of choice. I have only 1 terminal open and rebooted my system.

How can I fix it and could somebody please explain what happened, so that I can prevent/solve that problem on my own from now on?

You need to provide the protocol with the URL, this is no browser that prepends http:// automatically. Your script is correct, your call must be

node httpclient.js https://www.google.de

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM