node.js ExpressJS - 抛出未处理的错误事件

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

ExpressJS - throw er Unhandled error event

node.jsexpressnpm

提问by JR Galia

I created expressjs application using the following commands:

我使用以下命令创建了 expressjs 应用程序:

express -e folderName
npm install ejs --save
npm install

When I run the application with: node app.js, I have the following errors:

当我使用以下命令运行应用程序时node app.js,出现以下错误:

events.js:72
    throw er; // Unhandled 'error' event
          ^
Error: listen EADDRINUSE
    at errnoException (net.js:884:11)
    at Server._listen2 (net.js:1022:14)
    at listen (net.js:1044:10)
    at Server.listen (net.js:1110:5)
    at Object.<anonymous> (folderName/app.js:33:24)
    at Module._compile (module.js:456:26)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Function.Module.runMain (module.js:497:10)

How to fix it?

如何解决?

回答by Glowin

You had run another server use the same port like 8080.

您运行了另一台服务器,使用与 8080 相同的端口。

Maybe you had run node appin other shell, Please close it and run again.

也许你已经node app在其他shell中运行过,请关闭它并再次运行。

You can check PORT no. is available or not using

您可以检查端口号。可用或不使用

netstat -tulnp | grep <port no>

Alternatively, you can use lsof:

或者,您可以使用lsof

lsof -i :<port no>

回答by monical

We do get similar error when we sometimes run our express app. We have to follow the same in that case. We need to check if its running in any terminal. If you want to find and kill process, follow these steps:

当我们有时运行我们的 express 应用程序时,我们确实会遇到类似的错误。在这种情况下,我们必须遵循相同的原则。我们需要检查它是否在任何终端中运行。如果要查找并终止进程,请按照下列步骤操作:

  • ps aux | grep node
  • Find the process ID (second from the left):
  • kill -9 PRCOCESS_ID
  • ps辅助| grep 节点
  • 找到进程 ID(左起第二个):
  • kill -9 PRCOCESS_ID

OR

或者

Use a single command to close all the running node processes.

使用单个命令关闭所有正在运行的节点进程。

ps aux | awk '/node/{print }' | xargs kill -9

回答by TawabG

An instance is probably still running. This will fix it.

一个实例可能仍在运行。这将修复它。

killall node

Update: This command will only work on Linux/Ubuntu & Mac.

更新:此命令仅适用于 Linux/Ubuntu 和 Mac。

回答by CauselessEffect

If you're on Linux, this problem can also occur if Nodejs is not running as root.

如果您使用的是 Linux,如果 Nodejs 未以 root 身份运行,也会出现此问题。

Change from this:

从此改变:

nodejs /path/to/script.js

To this:

对此:

sudo nodejs /path/to/script.js

Just happened to me and none of the other suggestions here fixed it. Luckily I remembered the script was working the other day when running as root. Hope this helps someone!

只是发生在我身上,这里的其他建议都没有修复它。幸运的是,我记得前几天以 root 身份运行时脚本正在工作。希望这可以帮助某人!

Disclaimer:This probably isn't the best solution for a production environment. Starting your service as root may introduce some security holes to your server/application. In my case, this was a solution for a local service, but I'd encourage others to spend some more time trying to isolate the cause.

免责声明:这可能不是生产环境的最佳解决方案。以 root 身份启动您的服务可能会给您的服务器/应用程序带来一些安全漏洞。就我而言,这是本地服务的解决方案,但我鼓励其他人花更多时间尝试找出原因。

回答by Abhinav bhardwaj

This is because the port you are using to run the script is already in use. You have to stop all other nodes which are using that post. for that, you can check all node by

这是因为您用来运行脚本的端口已在使用中。您必须停止使用该帖子的所有其他节点。为此,您可以通过以下方式检查所有节点

ps -e

OR for node process only use ps -ef | grep nodeThis will give you the list of all node process with id

或仅用于节点进程,ps -ef | grep node这将为您提供带有 id 的所有节点进程的列表

to Kill all node process

杀死所有节点进程

sudo killall -9 node

Or for the specific id sudo kill -9 id

或者对于特定的 id sudo kill -9 id

回答by marcdahan

I fixed the bug by changing the port which was

我通过更改端口来修复错误

app.set('port', process.env.PORT || 3000);<br>

and changed to:

并改为:

app.set('port', process.env.PORT || 8080);<br>

回答by Fabien

The port Node is trying to use can be already used by another program. In my case it was ntop, which I had recently installed. I had to open http://localhost:3000/in a browser to realize it. Another way to find the process is given here.

节点尝试使用的端口可能已被另一个程序使用。就我而言,它是ntop,我最近安装了它。我必须在浏览器中打开http://localhost:3000/才能实现它。此处给出另一种查找过程的方法。

回答by Abolfazl Miadian

this means your file is running now. just enter below code and try again:

这意味着您的文件正在运行。只需输入以下代码并重试:

sudo pkill node

回答by Suneha Javid

If you want to use the same port number then type kill %in the terminal, which kills the current background process and frees up the port for further usage.

如果您想使用相同的端口号kill %,请在终端中输入,这会终止当前的后台进程并释放端口以供进一步使用。

回答by Jake McGuire

Close any other node servers that are running, even if they are in other terminal windows or running on different ports. That should fix the problem.

关闭正在运行的任何其他节点服务器,即使它们在其他终端窗口中或在不同端口上运行。那应该可以解决问题。