node.js express socket.io 端口 3000 正在使用中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28184795/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
node.js express socket.io port 3000 in use
提问by himahimahima
I've been following this(http://socket.io/get-started/chat/) tutorial on how to make a simple chat application using socket.io.
我一直在关注这个(http://socket.io/get-started/chat/)教程,了解如何使用 socket.io 制作一个简单的聊天应用程序。
I tried to however use Express to create it and I was wondering why port 3000 is already in use? The code below will not work unless I change the port number.
然而,我尝试使用 Express 来创建它,我想知道为什么端口 3000 已经在使用中?除非我更改端口号,否则下面的代码将不起作用。
/* Make the http server listen on port 3000. */
http.listen(3000, function(){
console.log('listening on *:3000');
});
Does express use the port to do other things like routing or something? Is there a simple way to find what is happening on that port?
express 是否使用端口来做其他事情,比如路由之类的?有没有一种简单的方法可以找到该端口上发生的事情?
I may also be doing something dodgy with my require things:
我也可能对我的要求做一些狡猾的事情:
var express = require('express');
var app = require('express')();
var http = require('http').Server(app);
var router = express.Router();
var io = require('socket.io')(http);
Thanks.
谢谢。
回答by Ethan Yanjia Li
I ran into this problem too and I solved it by this:
我也遇到了这个问题,我是这样解决的:
Do not use npm startto start your web app
不要npm start用于启动您的网络应用程序
Use node app.jsinstead
使用node app.js替代
回答by Brian Tovar
Try running:
尝试运行:
netstat -anp tcp | grep 3000
This should show you the name of the process that is using port 3000. Here's another issueon StackOverflow that covers this issue in more depth.
这应该会显示使用端口 3000 的进程的名称。这是StackOverflow 上的另一个问题,它更深入地介绍了这个问题。
回答by Gajen Sunthara
One of the best way to do this during the development would be through IDE where you can do comprehensive debugging and step through the code.
在开发过程中执行此操作的最佳方法之一是通过 IDE,您可以在其中进行全面的调试并逐步执行代码。
If you are using WebStorm, this works.
From run configurations -> Edit Configurations -> Nods.js and add the app.jsas the node parameter. See below arrow in the screenshots for more details.
如果您使用的是 WebStorm,则此方法有效。从运行配置 -> 编辑配置 -> Nods.js 并添加app.js作为节点参数。有关更多详细信息,请参阅屏幕截图中的下方箭头。
回答by Pedro Emilio Borrego Rached
I resolved the same problem with an express app doing this:
我用一个快速应用程序解决了同样的问题:
- Edit the file "yourap/bin/www"
find the line :
var port = normalizePort(process.env.PORT || '3000');
replace it by:
var port = normalizePort('XXXX');
- 编辑文件“yourap/bin/www”
找到行:
var port = normalizePort(process.env.PORT || '3000');
替换为:
var port = normalizePort('XXXX');
where XXXX is the port number you want to use
其中 XXXX 是您要使用的端口号
Then youre free to do npm start! xD
然后你就可以自由地做npm start 了!xD
回答by Dan0
I had (forgotten that I had) previously installed ntop, which by default also uses port 3000, and was therefore getting the same error as described here.
我(忘记了我)之前安装了ntop,默认情况下它也使用端口 3000,因此遇到与此处所述相同的错误。
As others have mentioned, use netstat or lsof to find the offending service (and prefix the command with sudo, to get the correct process name):
正如其他人所提到的,使用 netstat 或 lsof 查找有问题的服务(并在命令前加上 sudo,以获得正确的进程名称):
sudo lsof -P | grep ':3000'
- or -
- 或者 -
sudo netstat -anp tcp | grep 3000
On Ubuntu, the service is disabled with (simply):
在 Ubuntu 上,该服务被禁用(简单地):
service ntop stop
回答by cjmling
Similar to answer above to not use npm start.
类似于上面的回答不使用npm start.
I was using nodemon and with expressjs and expressjs generator. I was using nodemon to execute npm start, while npm start itself execute node ./NodeApp/bin/www
我正在使用 nodemon 以及 expressjs 和 expressjs 生成器。我使用 nodemon 执行npm start,而 npm start 本身执行node ./NodeApp/bin/www
So i edited to make nodemon to execute node ./NodeApp/bin/wwwby itself and that error go away.
所以我编辑让 nodemon 自己执行node ./NodeApp/bin/www并且错误消失了。
Conclusion
结论
Before
前
package.json
包.json
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node ./NodeApp/bin/www",
"build": "webpack --watch",
"dev": "nodemon --exec npm start"
},
After
后
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"build": "webpack --watch",
"dev": "nodemon --exec node ./NodeApp/bin/www"
},
So now I run my sever with npm run devand no more errors.
所以现在我运行我的服务器,npm run dev没有更多的错误。
回答by chavy
for me helps to make use 3000 || 3333, and it's fix the issue
对我来说有助于使用3000 || 3333,它解决了这个问题
回答by Utopia
I solved it by this:
我是这样解决的:
npm install shelljs
npm 安装 shelljs
and add code for kill nodejs process before start listen port
并在启动监听端口之前添加杀死 nodejs 进程的代码
var shell = require('shelljs');
shell.exec("pkill nodejs");
shell.exec("pkill node");
/* Make the http server listen on port 3000. */
http.listen(3000, function(){
console.log('listening on *:3000');
});


