Node.js抛出未处理的错误事件

时间:2014-03-26 07:12:50

标签: javascript node.js

我在Centos 6服务器上安装了node npm,我正在使用putty在服务器上运行命令。

节点在root上正确安装,并且在服务器的任何位置运行都很棒。

我的项目位于/ home / shaadise / public_html / Ikon

我创建了一个 hello.js 文件/ home / shaadise / public_html / Ikon

var http = require('http');

http.createServer(function (request, response) {
    response.writeHead(200, {'Content-Type': 'text/plain'});
    response.end('Hello World\n');
}).listen(8080);

console.log('Server started');
运行js时

root@vps [/home/shaadise/public_html/Ikon]# node hello.js
Server started

events.js:72
        throw er; // Unhandled 'error' event
              ^
Error: listen EADDRINUSE
    at errnoException (net.js:904:11)
    at Server._listen2 (net.js:1042:14)
    at listen (net.js:1064:10)
    at Server.listen (net.js:1138:5)
    at Object.<anonymous> (/home/shaadise/public_html/Ikon/hello.js:6:4)
    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)
root@vps [/home/shaadise/public_html/Ikon]#  throw er; // Unhandled 'error' event
-bash: throw: command not found
-bash: //: is a directory

问题:我必须在哪里放置我的节点js文件以及如何访问它?

我测试了运行命令:

root@vps [/home/shaadise/public_html/Ikon]# netstat -plnt | grep ':8080'
tcp        0      0 0.0.0.0:8080                0.0.0.0:*                   LISTEN      27111/nginx

5 个答案:

答案 0 :(得分:42)

Error: listen EADDRINUSE明确表示您或守护程序正在8080上运行另一个应用程序。

但是,要检查,请尝试在其他端口上运行吗?

-edit - 因为这会得到很多赞成,我想我会在其中添加一些额外的调试。

几乎所有node.js教程都默认使用端口8080进行运行。这是因为它类似于其他Web服务使用的默认端口80,例如Apache或NGinX。

为了确定另一个应用程序是否在同一个端口上运行,您可以使用netstat -a查看所有活动连接及其端口,然后grep该列表以查找与您的同一端口连接的任何进程Node.js应用程序。

运行Node应用程序的端口并不重要,只要它是一个空闲端口即可。最终,当您部署到生产环境中时,您将同步所使用的任何内容服务器(Apache / NGinX)以使用相同的端口。

答案 1 :(得分:3)

如果您使用的是基于Linux的系统,首先必须列出使用该特定端口的所有程序并将其终止(意味着停止它们)

例子:我想列出所有使用3000端口的程序

fuser 3000/tcp 

然后选择进程ID,该ID位于获取的文本行的右侧并发出kill命令

例如:如果进程ID的值为2345,那么命令将为

kill 2345

答案 2 :(得分:3)

获得此错误的常见情况是,当执行以下操作时:

  1. 启动某事
  2. 使用 Ctrl + z 并将其置于背景中
  3. 尝试重新启动
  4. 最好的方法是先尝试点击 Ctrl + c ,然后向应用程序发送信号(可能决定关机)。 你可以在这里读更多关于它的内容: What is the difference between Ctrl-z and Ctrl-c in the shell?

答案 3 :(得分:2)

服务器在后台运行;通常情况下,当你没有杀死这个过程时,它会被哄骗。 要解决此问题,您可以登上终端:

  

ps | grep&#39;节点&#39;

此代码将向您显示具有特定编号的进程,使用下一个代码来终止该进程:

  

kill -9&#34;具体数字&#34;

如果这不能正常工作,你可以使用sudo。

答案 4 :(得分:0)

如果关闭使用该端口的进程不能解决问题,请尝试以下解决方案。

安装以下软件包对我来说永远可以解决。

  

npm install ws@3.3.2 --save-dev --save-exact

在终端中运行以下命令:

  

echo fs.inotify.max_user_watches=524288 | sudo tee -a /etc/sysctl.conf && sudo sysctl -p

对于Arch Linux,将此行添加到/etc/sysctl.d/99-sysctl.conf:

fs.inotify.max_user_watches=524288

然后执行:

sysctl --system

https://github.com/guard/listen/wiki/Increasing-the-amount-of-inotify-watchers#the-technical-details