简体   繁体   English

Express js和socket io端口3000已经

[英]Express js and socket io port 3000 is already

I am trying to follow tutorial on how to use socket.io with express.js framework and node.js. 我正在尝试遵循有关如何在express.js框架和node.js中使用socket.io的教程。

Every tutorial I am following suggested I use the following lines to establish a connection in app.js 我遵循的每个教程建议我使用以下行在app.js中建立连接

var server = require('http').createServer(app);
var io = require('socket.io')(server);
server.listen(3000);


io.on('connection', function(client) {
   console.log('Client connected...');

   client.on('join', function(data) {
     console.log(data);
  });
})

This worked if I use port other than 3000 which I am having to run my application on http://localhost:3000/ . 如果我使用的端口不是3000,则必须在http:// localhost:3000 /上运行我的应用程序,这可以正常工作。 I get the error that Port 3000 already in use. 我收到端口3000已在使用的错误。

After debugging and looking at the code I think I have an idea of why is this happening. 调试并查看代码后,我认为我对为什么会这样有一个想法。 In ./bin/www.js file (created automatically by express js) we have the following lines: 在./bin/www.js文件(由express js自动创建)中,我们有以下几行:

/**
* Get port from environment and store in Express.
*/

var port = normalizePort(process.env.PORT || '3000');
app.set('port', port);
var server = http.createServer(app);
server.listen(port);

but I am not sure how to reuse this created server with same port in my app.js. 但我不确定如何在我的app.js中使用相同的端口重用此创建的服务器。 I am totally new to node.js. 我对node.js完全陌生。 How do I setup socket io on the express framework the right way? 如何以正确的方式在Express框架上设置socket io?

hello there please put your server.listen after socket connection like this 你好,请把你的server.listen在套接字连接后像这样

var app=require('expess')();
var server = require('http').createServer(app);
var io = require('socket.io')(server);

io.on('connection', function(client) {
   console.log('Client connected...');

   client.on('join', function(data) {
     console.log(data);
  });
})
server.listen(3000);

I hope this would work.Thanks 我希望这会起作用。

In order to kill any existing node process, you can run killall node command in your shell. 为了终止任何现有的节点进程,可以在shell中运行killall node命令。

Remove this line from your code, which is hardcoding the port number - 从您的代码中删除此行,这是对端口号进行硬编码的代码-

server.listen(3000);

and add something like this instead - 并添加类似这样的内容-

app.set('port', process.env.PORT || 3000);

app.listen(app.get('port'));

So your code would look something like - 所以您的代码看起来像-

var server = require('http').createServer(app);
var io = require('socket.io')(server);
// server.listen(3000);
app.set('port', process.env.PORT || 3000);


io.on('connection', function(client) {
   console.log('Client connected...');

   client.on('join', function(data) {
     console.log(data);
  });
});

server.listen(app.get('port'));

Now when your run your app again, it would boot on port 3000 by default or you can pass an environment variable (PORT) while starting the server like this to run on other ports. 现在,当您再次运行您的应用程序时,它将默认在端口3000上启动,或者您可以在启动服务器时像这样在其他端口上运行时传递环境变量(PORT)。

$ PORT=8080 node app.js

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM