Javascript Node.js引发未处理的错误事件
我已经在我的Centos 6服务器上安装了node、npm,并且我正在使用它在服务器上运行命令 节点已正确安装在根目录下,并在服务器的任何位置运行 我的项目在/home/shaadise/public_html/IkonJavascript Node.js引发未处理的错误事件,javascript,node.js,Javascript,Node.js,我已经在我的Centos 6服务器上安装了node、npm,并且我正在使用它在服务器上运行命令 节点已正确安装在根目录下,并在服务器的任何位置运行 我的项目在/home/shaadise/public_html/Ikon var http = require('http'); http.createServer(function (request, response) { response.writeHead(200, {'Content-Type': 'text/plain'});
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');
我已经创建了一个hello.js文件/home/shaadise/public\u 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
此
错误:侦听EADDRINUSE
明确表示您或守护进程正在8080上运行另一个应用程序
但是,要进行检查,请尝试在其他端口上运行
-edit-由于这会得到很多支持,我想我应该在其中添加一些额外的调试
几乎所有node.js教程都默认使用端口8080运行。这是因为它类似于其他web服务(如Apache或NGinX)使用的默认端口80
为了确定另一个应用程序是否在同一端口上运行,您可以使用netstat-a
查看所有活动连接及其端口,然后grep该列表以查找与Node.js应用程序在同一端口上连接的任何进程
节点应用程序运行在哪个端口上并不重要,只要它是一个空闲端口。最终,当您部署到生产环境中时,您将同步您正在使用的任何内容服务器(Apache/NGinX)以使用相同的端口。如果您使用的是基于Linux的系统,首先您必须列出使用该特定端口的所有程序并将其杀死(意味着停止它们) 我想列出所有使用3000端口的程序
fuser 3000/tcp
然后选择进程ID,它位于获取的文本行的右侧,并发出kill命令
示例:如果有一个值为2345的进程ID,那么该命令将
kill 2345
出现此错误的常见情况是执行以下操作:
服务器正在后台运行;通常情况下,当你不扼杀这个过程时,它就会发生。 要解决此问题,您可以在终端上: ps | grep“节点” 此代码将向您显示一个具有特定编号的进程,请使用下一代码终止该进程: 杀死-9“特定数量”
如果无法正常工作,您可以使用sudo。如果关闭使用该端口的进程无法解决问题,请尝试以下解决方案 安装下面的软件包为我永远修复了它。
npm安装ws@3.3.2--保存开发--准确保存
在终端中运行此命令:
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\u user\u watches=524288
然后执行:
sysctl——系统
另一个程序使用端口8080?没有,我只测试了一个程序在端口8080上运行。现在你可以把你的文件放在任何你想要的地方。检查用户权限一次。上面的答案解释了如何检查各个端口上运行的内容。然后,实现就是为web服务器使用标准节点实例化的一个例子