简体   繁体   English

错误:在nodejs中连接ECONNREFUSED问题

[英]Error: connect ECONNREFUSED issue in nodejs

while I am trying to execute the following code segment, I am getting the error... What is wrong with my code ? 当我尝试执行以下代码段时,出现错误...我的代码有什么问题?

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)

The code is to get the HTML contents from the URL 该代码是从URL获取HTML内容

function request1() {

  http.get('http://ggggg/status', function(res){
        var str = '';
        console.log('Response is '+res.statusCode);

        res.on('data', function (chunk) {
              console.log('BODY: ' + chunk);
               str += chunk;
         });

        res.on('end', function () {
             console.log(str);
        });

  });
}

request1()

The code for collecting the response seems fine, it's the request causing the problem. 收集响应的代码看起来不错,这是导致问题的请求。 You are not passing the authorisation header which is required. 您没有传递必需的授权标头。 If you visit the url in the browser you'll see what's required. 如果您在浏览器中访问该URL,则会看到所需内容。

This can also be verified by using curl: 也可以使用curl来验证:

$ curl http://integration.twosmiles.com/status
HTTP Basic: Access denied.

Also see: http://en.wikipedia.org/wiki/Basic_access_authentication 另请参阅: http : //en.wikipedia.org/wiki/Basic_access_authentication

Error : ECONNREFUSED 错误ECONNREFUSED


  • Is usually arised when you are trying to access a port which is already accessed by another program. 通常在您尝试访问已被另一个程序访问的端口时出现。
  • Double check on the port you are using & try using another port 仔细检查您使用的端口并尝试使用其他端口

Hope this helps ! 希望这会有所帮助

Your code seems good. 您的代码似乎不错。 Actually ECONNREFUSED error comes when the server is not able to connect to a client. 当服务器无法连接到客户端时,实际上会出现ECONNREFUSED错误。

In the code, http.get('http://ggggg/status', function(res){ … } 在代码中, http.get('http://ggggg/status', function(res){ … }

http.get() takes the first argument as the url which indicates that you are trying to connect that particular server which is currently listening(foreign host). http.get()将第一个参数用作url,它指示您正在尝试连接当前正在侦听的特定服务器(外部主机)。

For example: 例如:

var http = require('http');
function request1(){
  http.get('http://www.google.com', function cb1(a) {

    var str;
    a.on('data', function cb2(data) {
         //   console.log(data);
            str +=data;
    });
    a.on('end', function() {
            console.log(str);
    });
  });
}

request1();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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