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.
谢谢。
6 个解决方案
#1
19
I ran into this problem too and I solved it by this:
我也遇到了这个问题,我用这个来解决它:
Do not use npm start
to start your web app
不使用npm开始启动您的web应用程序
Use node app.js
instead
使用节点app.js而不是
#2
11
Try running:
尝试运行:
netstat -anp tcp | grep 3000
This should show you the name of the process that is using port 3000. Here's another issue on * that covers this issue in more depth.
这将显示使用端口3000的进程的名称。关于*的另一个问题将更深入地讨论这个问题。
#3
3
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.js
as the node parameter. See below arrow in the screenshots for more details.
如果你使用的是WebStorm,这是可行的。从运行配置->编辑配置->节点。并添加app.js作为节点参数。更多细节请参见截图中的箭头。
#4
1
I resolved the same problem with an express app doing this:
我用一个快递应用解决了同样的问题:
- Edit the file "yourap/bin/www"
- 编辑该文件“yourap / bin / www”
-
find the line :
找到:
var port = normalizePort(process.env.PORT || '3000');
var = normalizePort港(process.env。港| | ' 3000 ');
-
replace it by:
换成:
var port = normalizePort('XXXX');
var = normalizePort港(“XXXX”);
where XXXX is the port number you want to use
您想要使用的端口号XXXX在哪里
Then youre free to do npm start! xD
那你就可以开始npm了!xD
#5
1
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
#6
0
Similar to answer above to not use npm start
.
类似以上的回答不使用npm启动。
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/www
by itself and that error go away.
所以我编辑了nodemon来执行node ./NodeApp/bin/www,这个错误消失了。
Conclusion
结论
Before
之前
package.json
package.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 dev
and no more errors.
现在我用npm run dev运行服务器,不再出现错误。
#1
19
I ran into this problem too and I solved it by this:
我也遇到了这个问题,我用这个来解决它:
Do not use npm start
to start your web app
不使用npm开始启动您的web应用程序
Use node app.js
instead
使用节点app.js而不是
#2
11
Try running:
尝试运行:
netstat -anp tcp | grep 3000
This should show you the name of the process that is using port 3000. Here's another issue on * that covers this issue in more depth.
这将显示使用端口3000的进程的名称。关于*的另一个问题将更深入地讨论这个问题。
#3
3
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.js
as the node parameter. See below arrow in the screenshots for more details.
如果你使用的是WebStorm,这是可行的。从运行配置->编辑配置->节点。并添加app.js作为节点参数。更多细节请参见截图中的箭头。
#4
1
I resolved the same problem with an express app doing this:
我用一个快递应用解决了同样的问题:
- Edit the file "yourap/bin/www"
- 编辑该文件“yourap / bin / www”
-
find the line :
找到:
var port = normalizePort(process.env.PORT || '3000');
var = normalizePort港(process.env。港| | ' 3000 ');
-
replace it by:
换成:
var port = normalizePort('XXXX');
var = normalizePort港(“XXXX”);
where XXXX is the port number you want to use
您想要使用的端口号XXXX在哪里
Then youre free to do npm start! xD
那你就可以开始npm了!xD
#5
1
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
#6
0
Similar to answer above to not use npm start
.
类似以上的回答不使用npm启动。
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/www
by itself and that error go away.
所以我编辑了nodemon来执行node ./NodeApp/bin/www,这个错误消失了。
Conclusion
结论
Before
之前
package.json
package.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 dev
and no more errors.
现在我用npm run dev运行服务器,不再出现错误。