Javascript node.js 错误:连接 ECONNREFUSED;来自服务器的响应

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35199384/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 17:22:54  来源:igfitidea点击:

node.js Error: connect ECONNREFUSED; response from server

javascriptnode.jsconnect

提问by Marco Ghieri

I have a problem with this little program:

这个小程序有问题:

var http = require("http");
var request = http.request({
    hostname: "localhost",
    port: 8000,
    path: "/",
    method: "GET"
}, function(response) {
    var statusCode = response.statusCode;
    var headers = response.headers;
    var statusLine = "HTTP/" + response.httpVersion + " " +statusCode + " " + http.STATUS_CODES[statusCode];
    console.log(statusLine);
    for (header in headers) {
        console.log(header + ": " + headers[header]);
    }
    console.log();
    response.setEncoding("utf8");
    response.on("data", function(data) {
        process.stdout.write(data);
    });
    response.on("end", function() {
        console.log();
    });
});

The result in console is this:

控制台中的结果是这样的:

events.js:141
      throw er; // Unhandled 'error' event
      ^

Error: connect ECONNREFUSED 127.0.0.1:8000
    at Object.exports._errnoException (util.js:870:11)
    at exports._exceptionWithHostPort (util.js:893:20)
    at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1063:14)

I do not understand why this happens.

我不明白为什么会发生这种情况。

回答by Piyush Sagar

From your code, It looks like your file contains code that makes get request to localhost (127.0.0.1:8000).

从您的代码来看,您的文件似乎包含向 localhost (127.0.0.1:8000) 发出 get 请求的代码。

The problem might be you have not created server on your local machine which listens to port 8000.

问题可能是您尚未在本地计算机上创建侦听端口 8000 的服务器。

For that you have to set up server on localhost which can serve your request.

为此,您必须在 localhost 上设置可以满足您的请求的服务器。

  1. Create server.js

    var express = require('express');
    var app = express();
    
    app.get('/', function (req, res) {
      res.send('Hello World!'); // This will serve your request to '/'.
    });
    
    app.listen(8000, function () {
      console.log('Example app listening on port 8000!');
     });
    
  2. Run server.js : node server.js

  3. Run file that contains code to make request.

  1. 创建 server.js

    var express = require('express');
    var app = express();
    
    app.get('/', function (req, res) {
      res.send('Hello World!'); // This will serve your request to '/'.
    });
    
    app.listen(8000, function () {
      console.log('Example app listening on port 8000!');
     });
    
  2. 运行 server.js :节点 server.js

  3. 运行包含发出请求的代码的文件。

回答by Ahmad Zahabi

Please use [::1] instead of localhost, and make sure that the port is correct, and put the port inside the link.

请使用 [::1] 而不是 localhost,并确保端口正确,并将端口放在链接中。

const request = require('request');

   let json = {
        "id": id,
        "filename": filename
    };
    let options = {
        uri: "http://[::1]:8000" + constants.PATH_TO_API,
        // port:443,
        method: 'POST',
        json: json
    };
    request(options, function (error, response, body) {
        if (error) {
            console.error("httpRequests : error " + error);
        }
        if (response) {
            let statusCode = response.status_code;
            if (callback) {
                callback(body);
            }
        }
    });

回答by user web services

I had the same problem on my mac, but in my case, the problem was that I did not run the database (sudo mongod) before; the problem was solved when I first ran the mondo sudod on the console and, once it was done, on another console, the connection to the server ...

我在我的 mac 上遇到了同样的问题,但就我而言,问题是我之前没有运行数据库(sudo mongod);当我第一次在控制台上运行 mondo sudod 时,问题就解决了,一旦完成,在另一个控制台上,连接到服务器......

回答by Timar Ivo Batis

i ran the local mysql database, but not in administrator mode, which threw this error

我运行了本地 mysql 数据库,但不是在管理员模式下,这引发了这个错误

回答by Gaurab Agarwala

use a proxy property in your code it should work just fine

在您的代码中使用代理属性它应该可以正常工作

const https = require('https');
const request = require('request');

request({
    'url':'https://teamtreehouse.com/chalkers.json',
    'proxy':'http://xx.xxx.xxx.xx'
    },
    function (error, response, body) {
        if (!error && response.statusCode == 200) {
            var data = body;
            console.log(data);
        }
    }
);