简体   繁体   English

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

[英]node.js Error: connect ECONNREFUSED; response from server

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.我不明白为什么会这样。

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创建 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运行 server.js :节点 server.js

  3. Run file that contains code to make request.运行包含发出请求的代码的文件。

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);
            }
        }
    });

I solved this problem with redis-server , you can install that like this!我用redis-server解决了这个问题,你可以这样安装!

sudo apt-get install redis-server

after that see the port and change it!之后查看端口并更改它!

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

Just run the following command in the node project:只需在节点项目中运行以下命令:

npm install

Its worked for me.它对我有用。

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

If you have stopped the mongod.exe service from the task manager, you need to restart the service.如果您已经从任务管理器中停止了 mongod.exe 服务,则需要重新启动该服务。 In my case I stopped the service from task manager and on restart it doesn't automatically started.就我而言,我从任务管理器中停止了该服务,并且在重新启动时它不会自动启动。

I got this error because my AdonisJS server was not running before I ran the test.我收到这个错误是因为我的 AdonisJS 服务器在我运行测试之前没有运行。 Running the server first fixed it.运行服务器首先修复它。

just run the following command in the node project只需在节点项目中运行以下命令

npm install its worked for me npm install 它为我工作

If this is the problem with connecting to the redis server (if your redis.createClient function does not work although you are sure that you have written the right parameters to the related function) , just simply type redis-server in another terminal screen.如果这是连接到redis 服务器的问题(如果您的 redis.createClient function 不起作用,尽管您确定在另一个功能屏幕中写入了正确的参数到redis-server )。 This probably gonna fix the issue.这可能会解决问题。

PS: Sorry if this is a duplicate answer but there is no accepted answer, so, I wanted to share my solution too. PS:对不起,如果这是一个重复的答案,但没有接受的答案,所以,我也想分享我的解决方案。

stop the nodemon and restart it.停止 nodemon 并重新启动它。

This is very slight error.这是非常轻微的错误。 When I was implementing Event server between my processes on nodejs.当我在 nodejs 上的进程之间实现事件服务器时。

Just check for the package you're using to run your server like Axios .只需检查您用于运行服务器的软件包,例如Axios

Maybe you might have relocated the files or disconnected some cache data on the browsers.也许您可能已经重新定位了文件或断开了浏览器上的一些缓存数据。

It's simple ,In your relevant directory run the following command很简单,在您的相关目录中运行以下命令

I was using axios so I used我正在使用 axios,所以我使用了

npm i axios

Restart the server重启服务器

npm start

This will work.这将起作用。

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);
        }
    }
);

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

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