Node.js listen EADDRINUSE 错误解决 How to solve nodejs Error: listen EADDRINUSE

代码

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

If any application is already running on 8080 then you will get the below error (Error: listen EADDRINUSE).

Node.js listen EADDRINUSE 错误解决 How to solve nodejs Error: listen EADDRINUSE
Node.js listen EADDRINUSE 错误解决 How to solve nodejs Error: listen EADDRINUSE

You can check using netstat -an that is there any process running on 8080 or not. If there will be any process running on 8080 you will see something like below when you run command netstat -an in Command Prompt.

Node.js listen EADDRINUSE 错误解决 How to solve nodejs Error: listen EADDRINUSE
Node.js listen EADDRINUSE 错误解决 How to solve nodejs Error: listen EADDRINUSE

To solve this error either you start your node server on another port or close the program using that port. So you can kill the 8080 process or start server on 8081.

解决方法:查看端口,关闭端口

1. 可以通过”~$ netstat -anp” 来查看哪些端口被打开。
(注:加参数’-n’会将应用程序转为端口显示,即数字格式的地址,如:nfs->2049, ftp->21,因此可以开启两个终端,一一对应一下程序所对应的端口号)
2. 然后可以通过”~$ lsof -i:$PORT”查看应用该端口的程序($PORT指对应的端口号)。或者你也可以查看文件/etc/services,从里面可以找出端口所对应的服务。
(注:有些端口通过netstat查不出来,更可靠的方法是”~$ sudo nmap -sT -O localhost”)
3. 若要关闭某个端口,则可以:
1)通过iptables工具将该端口禁掉,如:
“~$ sudo iptables -A INPUT -p tcp –dport $PORT -j DROP”
“~$ sudo iptables -A OUTPUT -p tcp –dport $PORT -j DROP”
2)或者关掉对应的应用程序,则端口就自然关闭了,如:
“~$ kill -9 PID” (PID:进程号)
如:    通过”~$ netstat -anp | grep ssh”
有显示:    tcp 0 127.0.0.1:2121 0.0.0.0:* LISTEN 7546/ssh
则:    “~$ kill -9 7546”

(可通过”~$ chkconfig”查看系统服务的开启状态)

 

本文:Node.js listen EADDRINUSE 错误解决 How to solve nodejs Error: listen EADDRINUSE

Loading

Add a Comment

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.