Node.js 未处理的“错误”事件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16331783/
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-09-02 14:23:18  来源:igfitidea点击:

Node.js unhandled 'error' event

javascriptnode.jshttpexpressport

提问by ana

I have written a simple code and save it in file try.js .

我编写了一个简单的代码并将其保存在文件 try.js 中。

var http = require('http');
var makeRequest = function(message) {
  var options = {
    host: 'localhost', port: 8080, path: '/', method: 'POST'
  }
  var request = http.request(options, function(response){
    response.on('data', function(data){
      console.log(data);
    });
  });

  request.write(message);
  request.end();
}
makeRequest("Here's looking at you, kid.");

When I run it through terminal it give an error

当我通过终端运行它时,它会出错

throw er; // Unhandled 'error' event,
Error: connect ECONNREFUSED
at errnoException (net.js:884:11)
at Object.afterConnect [as oncomplete] (net.js:875:19)error' event

回答by MVCaraiman

For me, the problem was another instance was running. Search for other node instance running and kill it. It should resume properly after.

对我来说,问题是另一个实例正在运行。搜索正在运行的其他节点实例并杀死它。之后它应该可以正常恢复。

回答by Damian Krawczyk

This is happening because nothing is listening on localhost:8080. Here are the examples how to handle request errors How to catch http client request exceptions in node.js

发生这种情况是因为没有在 localhost:8080 上监听。以下是如何处理请求错误的示例 如何在 node.js 中捕获 http 客户端请求异常

回答by KARTHIKEYAN.A

Another server is running on same port 8080, so you need to kill and start your instance.

另一台服务器在同一端口 8080 上运行,因此您需要终止并启动您的实例。

find running instance

查找正在运行的实例

$ netstat -nlp | grep 8080

(Not all processes could be identified, non-owned process info
 will not be shown, you would have to be root to see it all.)
tcp6       0      0 :::8881                 :::*                    LISTEN      28207/node    

kill existing server instance using pid

使用 pid 杀死现有的服务器实例

$ kill -9 28207

start instance

启动实例

$ node server.js 

回答by SM Chinna

change port number. Because your port is already is running.

更改端口号。因为您的端口已经在运行。

port number like : 8000,8001,3000,3001,8080 ....

端口号如:8000,8001,3000,3001,8080 ....

 app.listen(3030,()=>{})

(or) Kill your port number. If you are using Ubuntu OS to follow this steps.

(或)杀死您的端口号。如果您使用的是 Ubuntu 操作系统,请按照以下步骤操作。

  1. To Find PID or TCP using terminal using this command: sudo netstat -ap | grep :"port number"
  2. To Kill the process using this command: kill -9 "process_id or tcp_id"
  3. To Run your NodeJs
  1. 使用以下命令使用终端查找 PID 或 TCP: sudo netstat -ap | grep :"端口号"
  2. 要使用此命令终止进程: kill -9 "process_id or tcp_id"
  3. 运行你的 NodeJs

回答by Ashita Gaur

Another server is running on the same port 8080 so change the port in this code or kill the port 8080 and restart it.

另一台服务器正在同一端口 8080 上运行,因此更改此代码中的端口或终止端口 8080 并重新启动它。

回答by suhadev venkatesh

A different process is already running in the same port. Either kill the running process or change the port number in the current code. Your issue will be fixed!

不同的进程已经在同一个端口中运行。要么终止正在运行的进程,要么更改当前代码中的端口号。您的问题将得到解决!