nodejs EADDRINUSE 错误
nodejs EADDRINUSE error
我开始使用 [cloud9][1] 并尝试托管我的 nodejs 应用程序。
当我尝试 运行 我的应用程序时,它抛出以下错误:
重要:在
中使用 process.env.PORT 作为端口,process.env.IP 作为主机
Important: use process.env.PORT as the port and process.env.IP as the host in your scripts!
debugger listening on port 15454
8080
0.0.0.0
events.js:72
throw er; // Unhandled 'error' event
^
Error: listen EADDRINUSE
at errnoException (net.js:905:11)
at Server._listen2 (net.js:1043:14)
at listen (net.js:1065:10)
at Server.listen (net.js:1147:9)
at exports.lookup.callback (dns.js:72:18)
at process._tickCallback (node.js:442:13)
at Module.runMain [as _onTimeout] (module.js:499:11)
at Timer.listOnTimeout [as ontimeout] (timers.js:112:15)
我的app.js:
var app = require('express')();
//var app = express();
//var http = require('http');
var server = require('http').Server(app);
var io = require('socket.io')(server);
app.set('port', process.env.PORT || 3000);
var port = app.get('port');
console.log(port);
server.listen(port, process.env.IP);
// routing
app.get('/', function (req, res) {
res.sendfile(__dirname + '/index.html');
});
// usernames which are currently connected to the chat
var usernames = {};
// rooms which are currently available in chat
var rooms = [];
io.sockets.on('connection', function (socket) {
socket.on('adduser', function (username, room) {
socket.username = username;
socket.room = room;
usernames[username] = username;
socket.join(room);
socket.emit('updatechat', 'SERVER', 'You are connected. Start chatting');
socket.broadcast.to(room).emit('updatechat', 'SERVER', username + ' has connected to this room');
});
socket.on('createroom', function () {
var new_room = (""+Math.random()).substring(2,7);
rooms.push(new_room);
socket.emit('updatechat', 'SERVER', 'Your room is ready, invite someone using this ID:' + new_room);
socket.emit('roomcreated', new_room);
});
// when the client emits 'sendchat', this listens and executes
socket.on('sendchat', function (data) {
// we tell the client to execute 'updatechat' with 2 parameters
io.sockets. in (socket.room).emit('updatechat', socket.username, data);
});
// when the user disconnects.. perform this
socket.on('disconnect', function () {
// remove the username from global usernames list
delete usernames[socket.username];
// update list of users in chat, client-side
io.sockets.emit('updateusers', usernames);
// echo globally that this client has left
if(socket.username !== undefined){
socket.broadcast.emit('updatechat', 'SERVER', socket.username + ' has disconnected');
socket.leave(socket.room);
}
});
});
我尝试在 8080 处寻找已经 运行 的任何进程,这是输出
user@chat_room:~/workspace (master) $ ps ax | grep node
14154 pts/1 S+ 0:00 grep --color=auto node
如果我尝试使用其他端口,则无法获取套接字文件,它会抛出 404 错误:
<script src="/socket.io/socket.io.js"></script>
这在本地工作正常。
编辑
打开端口:
user@chat_room:~/workspace (master) $ netstat --listen
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 *:mysql *:* LISTEN
tcp 0 0 localhost:17123 *:* LISTEN
tcp6 0 0 [::]:http-alt [::]:* LISTEN
tcp6 0 0 [::]:ssh [::]:* LISTEN
Active UNIX domain sockets (only servers)
Proto RefCnt Flags Type State I-Node Path
unix 2 [ ACC ] STREAM LISTENING 353035357 /tmp/tmux-1000/cloud91.8
unix 2 [ ACC ] STREAM LISTENING 351539622 /home/ubuntu/lib/mysql/socket/mysql.sock
unix 2 [ ACC ] STREAM LISTENING 352204732 /home/ubuntu/.c9/1312164/collab.sock
user@chat_room:~/workspace (master) $ netstat -vatn
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN
tcp 0 0 127.0.0.1:17123 0.0.0.0:* LISTEN
tcp 0 0 127.0.0.1:37186 127.0.0.1:15455 TIME_WAIT
tcp 0 0 127.0.0.1:15454 127.0.0.1:59371 TIME_WAIT
tcp6 0 0 :::8080 :::* LISTEN
tcp6 0 0 :::22 :::* LISTEN
tcp6 0 400 172.17.0.248:22 10.240.179.70:43154 ESTABLISHED
user@chat_room:~/workspace (master) $ netstat -vat
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 *:mysql *:* LISTEN
tcp 0 0 localhost:17123 *:* LISTEN
tcp 0 0 localhost:37186 localhost:15455 TIME_WAIT
tcp 0 0 localhost:15454 localhost:59371 TIME_WAIT
tcp6 0 0 [::]:http-alt [::]:* LISTEN
tcp6 0 0 [::]:ssh [::]:* LISTEN
tcp6 0 0 user-chat_:ssh 10.240.179.70:43154 ESTABLISHED
user@chat_room:~/workspace (master) $ lsof -i
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
mysqld 2316 ubuntu 10u IPv4 351539621 0t0 TCP *:mysql (LISTEN)
apache2 2374 ubuntu 4u IPv6 351539675 0t0 TCP *:http-alt (LISTEN)
apache2 2381 ubuntu 4u IPv6 351539675 0t0 TCP *:http-alt (LISTEN)
apache2 2382 ubuntu 4u IPv6 351539675 0t0 TCP *:http-alt (LISTEN)
apache2 2383 ubuntu 4u IPv6 351539675 0t0 TCP *:http-alt (LISTEN)
apache2 2384 ubuntu 4u IPv6 351539675 0t0 TCP *:http-alt (LISTEN)
apache2 2385 ubuntu 4u IPv6 351539675 0t0 TCP *:http-alt (LISTEN)
vfs-worke 13915 ubuntu 13u IPv4 352221394 0t0 TCP localhost:17123 (LISTEN)
我会说错误说明了一切:
Important: use process.env.PORT as the port and process.env.IP as the host in your scripts!
您的环境希望您分别使用 process.env.PORT
和 process.env.IP
作为端口和 ip(是 cloud9 吗?),但是在这一行上您使用了另一个环境变量:
app.set('port', process.env.app_port || 8080)
修复:
app.set('port', process.env.PORT || 8080)
(一般情况下,EADDRINUSE 错误是因为另一个进程正在侦听该端口)
对于 404 错误
编辑:之前发布的代码适用于旧版本的 Express。参见 http://socket.io/docs/#using-with-express-3/4
您的代码中是否有可能请求 listen()
到源中两个位置的同一端口?我没有看到两个 listen 调用,但我对 express 或 sockets 不是很熟悉,不知道是否有另一个函数执行此操作......尽管在不同的情况下,它发生在我身上一次。
使用命令列出端口 8080 上的进程 运行:
lsof-i:8080,
通过使用 运行 个进程的 PID 使用命令杀死使用端口 8080 的实例:
kill -9 PID
我开始使用 [cloud9][1] 并尝试托管我的 nodejs 应用程序。 当我尝试 运行 我的应用程序时,它抛出以下错误:
重要:在
中使用 process.env.PORT 作为端口,process.env.IP 作为主机Important: use process.env.PORT as the port and process.env.IP as the host in your scripts!
debugger listening on port 15454
8080
0.0.0.0
events.js:72
throw er; // Unhandled 'error' event
^
Error: listen EADDRINUSE
at errnoException (net.js:905:11)
at Server._listen2 (net.js:1043:14)
at listen (net.js:1065:10)
at Server.listen (net.js:1147:9)
at exports.lookup.callback (dns.js:72:18)
at process._tickCallback (node.js:442:13)
at Module.runMain [as _onTimeout] (module.js:499:11)
at Timer.listOnTimeout [as ontimeout] (timers.js:112:15)
我的app.js:
var app = require('express')();
//var app = express();
//var http = require('http');
var server = require('http').Server(app);
var io = require('socket.io')(server);
app.set('port', process.env.PORT || 3000);
var port = app.get('port');
console.log(port);
server.listen(port, process.env.IP);
// routing
app.get('/', function (req, res) {
res.sendfile(__dirname + '/index.html');
});
// usernames which are currently connected to the chat
var usernames = {};
// rooms which are currently available in chat
var rooms = [];
io.sockets.on('connection', function (socket) {
socket.on('adduser', function (username, room) {
socket.username = username;
socket.room = room;
usernames[username] = username;
socket.join(room);
socket.emit('updatechat', 'SERVER', 'You are connected. Start chatting');
socket.broadcast.to(room).emit('updatechat', 'SERVER', username + ' has connected to this room');
});
socket.on('createroom', function () {
var new_room = (""+Math.random()).substring(2,7);
rooms.push(new_room);
socket.emit('updatechat', 'SERVER', 'Your room is ready, invite someone using this ID:' + new_room);
socket.emit('roomcreated', new_room);
});
// when the client emits 'sendchat', this listens and executes
socket.on('sendchat', function (data) {
// we tell the client to execute 'updatechat' with 2 parameters
io.sockets. in (socket.room).emit('updatechat', socket.username, data);
});
// when the user disconnects.. perform this
socket.on('disconnect', function () {
// remove the username from global usernames list
delete usernames[socket.username];
// update list of users in chat, client-side
io.sockets.emit('updateusers', usernames);
// echo globally that this client has left
if(socket.username !== undefined){
socket.broadcast.emit('updatechat', 'SERVER', socket.username + ' has disconnected');
socket.leave(socket.room);
}
});
});
我尝试在 8080 处寻找已经 运行 的任何进程,这是输出
user@chat_room:~/workspace (master) $ ps ax | grep node
14154 pts/1 S+ 0:00 grep --color=auto node
如果我尝试使用其他端口,则无法获取套接字文件,它会抛出 404 错误:
<script src="/socket.io/socket.io.js"></script>
这在本地工作正常。
编辑 打开端口:
user@chat_room:~/workspace (master) $ netstat --listen
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 *:mysql *:* LISTEN
tcp 0 0 localhost:17123 *:* LISTEN
tcp6 0 0 [::]:http-alt [::]:* LISTEN
tcp6 0 0 [::]:ssh [::]:* LISTEN
Active UNIX domain sockets (only servers)
Proto RefCnt Flags Type State I-Node Path
unix 2 [ ACC ] STREAM LISTENING 353035357 /tmp/tmux-1000/cloud91.8
unix 2 [ ACC ] STREAM LISTENING 351539622 /home/ubuntu/lib/mysql/socket/mysql.sock
unix 2 [ ACC ] STREAM LISTENING 352204732 /home/ubuntu/.c9/1312164/collab.sock
user@chat_room:~/workspace (master) $ netstat -vatn
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN
tcp 0 0 127.0.0.1:17123 0.0.0.0:* LISTEN
tcp 0 0 127.0.0.1:37186 127.0.0.1:15455 TIME_WAIT
tcp 0 0 127.0.0.1:15454 127.0.0.1:59371 TIME_WAIT
tcp6 0 0 :::8080 :::* LISTEN
tcp6 0 0 :::22 :::* LISTEN
tcp6 0 400 172.17.0.248:22 10.240.179.70:43154 ESTABLISHED
user@chat_room:~/workspace (master) $ netstat -vat
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 *:mysql *:* LISTEN
tcp 0 0 localhost:17123 *:* LISTEN
tcp 0 0 localhost:37186 localhost:15455 TIME_WAIT
tcp 0 0 localhost:15454 localhost:59371 TIME_WAIT
tcp6 0 0 [::]:http-alt [::]:* LISTEN
tcp6 0 0 [::]:ssh [::]:* LISTEN
tcp6 0 0 user-chat_:ssh 10.240.179.70:43154 ESTABLISHED
user@chat_room:~/workspace (master) $ lsof -i
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
mysqld 2316 ubuntu 10u IPv4 351539621 0t0 TCP *:mysql (LISTEN)
apache2 2374 ubuntu 4u IPv6 351539675 0t0 TCP *:http-alt (LISTEN)
apache2 2381 ubuntu 4u IPv6 351539675 0t0 TCP *:http-alt (LISTEN)
apache2 2382 ubuntu 4u IPv6 351539675 0t0 TCP *:http-alt (LISTEN)
apache2 2383 ubuntu 4u IPv6 351539675 0t0 TCP *:http-alt (LISTEN)
apache2 2384 ubuntu 4u IPv6 351539675 0t0 TCP *:http-alt (LISTEN)
apache2 2385 ubuntu 4u IPv6 351539675 0t0 TCP *:http-alt (LISTEN)
vfs-worke 13915 ubuntu 13u IPv4 352221394 0t0 TCP localhost:17123 (LISTEN)
我会说错误说明了一切:
Important: use process.env.PORT as the port and process.env.IP as the host in your scripts!
您的环境希望您分别使用 process.env.PORT
和 process.env.IP
作为端口和 ip(是 cloud9 吗?),但是在这一行上您使用了另一个环境变量:
app.set('port', process.env.app_port || 8080)
修复:
app.set('port', process.env.PORT || 8080)
(一般情况下,EADDRINUSE 错误是因为另一个进程正在侦听该端口)
对于 404 错误
编辑:之前发布的代码适用于旧版本的 Express。参见 http://socket.io/docs/#using-with-express-3/4
您的代码中是否有可能请求 listen()
到源中两个位置的同一端口?我没有看到两个 listen 调用,但我对 express 或 sockets 不是很熟悉,不知道是否有另一个函数执行此操作......尽管在不同的情况下,它发生在我身上一次。
使用命令列出端口 8080 上的进程 运行: lsof-i:8080, 通过使用 运行 个进程的 PID 使用命令杀死使用端口 8080 的实例: kill -9 PID