- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我开始使用 [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
关于javascript - nodejs EADDRINUSE 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28728548/
我试图挽救在bind语句中两次尝试使用相同地址时ruby引发的异常。 documentation不是很有帮助。 这就是我想要的: require 'socket' s = UDPSocket.new(
假设您有 2 个 ASP.NET 核心项目;一个 API 和一个网站。我想使用以下 url 在本地(同时,因为该网站使用 api)处理这些问题:devapi.website.com 和 dev.web
我在 Android 上遇到 TCP 服务器问题。服务器必须管理多个传入连接,目前来自同一用户。我收到以下错误: 02-06 17:37:44.800: W/System.err(9859): jav
我已经在Ubuntu 10.04上配置并安装了ouchdb 1.6.1。在配置和安装时说成功完成了。但是,当我启动沙发床时,它给了我错误。 首次启动时。 Apache CouchDB 1.6.1 (L
IOLib 允许创建一个被动套接字来监听客户端的连接,在调用 listen 之前,我们需要调用 (bind-address) 将套接字绑定(bind)到指定的地址/端口。 嗯,问题是我第一次把sock
我正在做套接字编程。我引用了以下链接: http://examples.javacodegeeks.com/android/core/socket-core/android-socket-exampl
我开始使用 [cloud9][1] 并尝试托管我的 nodejs 应用程序。当我尝试运行我的应用程序时,它抛出以下错误: 重要:使用 process.env.PORT 作为端口,使用 process.
尝试构建实验性 CRUD 应用程序,但连接到数据库时出现问题。 使用 mLab 免费数据库。无论指定哪个端口,错误都是 EADDRINUSE::: 如何关闭可能使用这些端口的所有 Node 实例? 最
我有一个简单的 gulpfile.js: var gulp = require('gulp'); var sass = require('gulp-sass'); var connect
我有来自 CSgo roullete 的脚本。当我尝试运行它时显示此错误 errno: 'ECONNREFUSED', syscall: 'connect', address: '
这是我的 Gruntfile: module.exports = function(grunt) { grunt.initConfig({ pkg: grunt.file.readJSON
在我的 C++ 应用程序中,我将::bind() 用于 UDP 套接字,但在极少数情况下,由于连接丢失而重新连接后,即使重试多次,我也会收到错误号 EADDRINUSE。将接收数据的 UDP 连接的另
这个问题在这里已经有了答案: 关闭 11 年前。 Possible Duplicates: Releasing bound ports on process exit difference betw
下面是我的test fixture的精华—— SetUp() { g_listen_sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
我使用 Ubuntu 20.04、Nodemon 2.0.4、Node 14.9.0 和 Express 4.17.1。每次保存我的代码时,我都会收到此错误: events.js:291
我正在开发一个 google glass/android 应用程序。它是一个视频流应用程序,具有服务器/客户端设置,其中电话/眼镜是服务器,并将 pc 与播放视频的 session 描述 Hook 。
我有一个系统,其中在后台运行的服务器进程由控制程序控制。 控制程序是一个简单的脚本,它执行一个命令然后退出。对于 Run 命令,它会创建一个新的服务器进程;对于其他(包括关机),它通过保留的控制端口将
好吧,这真的很奇怪,我在实现一个简单的服务器时得到了 EADDRINUSE, 我已经更改了端口 寻找已经运行的node js进程 尝试使用 killall -9 node 和 root 通过sudo
我有一个使用express的测试应用程序,它在server.listen(80)上崩溃:错误:监听EADDRINUSE。我尝试使用 killall -9 node 终止所有 Node 进程,但没有进程
如果我使用端口 80 运行服务器,并且我尝试使用 XMLHttpRequest我收到此错误:错误:监听 EADDRINUSE 如果我想在端口 80 上运行服务器时发出请求,为什么 NodeJS 会出现
我是一名优秀的程序员,十分优秀!