node.js nodemon + express,监听端口 =?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23647593/
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
nodemon + express, listen port =?
提问by tivoni
I create a simple node project using express:
我使用 express 创建了一个简单的节点项目:
npm install -g express-generator
express test
cd test/ && npm install
PORT=3000 npm start
So this gets the test app up and running on port 3000. Great. Now I'd like to use nodemonto run this project. I've installed it:
所以这让测试应用程序在端口 3000 上启动并运行。太好了。现在我想用它nodemon来运行这个项目。我已经安装了它:
npm install -g nodemon
In the gihub README it is run the same way as node. This is a bit confusing, because the new way of starting node is npm startnot node. So I tried:
在 gihub README 中,它的运行方式与 node.js 相同。这有点令人困惑,因为启动节点的新方式npm start不是node. 所以我试过:
$ PORT=3000 nodemon ./app.js
13 May 23:41:16 - [nodemon] v1.0.18
13 May 23:41:16 - [nodemon] to restart at any time, enter `rs`
13 May 23:41:16 - [nodemon] watching: *.*
13 May 23:41:16 - [nodemon] starting `node ./app.js`
13 May 23:41:16 - [nodemon] clean exit - waiting for changes before restart
But when I try to connect, there's nothing there. I confirmed that with:
但是当我尝试连接时,那里什么也没有。我证实了这一点:
lsof -i TCP:3000
Which returned nothing. Normally (with npm start) it returns:
什么都没有返回。通常(与npm start)它返回:
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
node 18746 user 10u IPv4 433546 0t0 TCP *:3000 (LISTEN)
Can anyone tell whats wrong here?
How is it possible to get the app to listen on the specified port with nodemon?
谁能告诉这里有什么问题?如何让应用程序监听指定的端口nodemon?
my setup:
我的设置:
npm -v
1.3.21
node -v
v0.10.24
nodemon -v
v1.0.18
express -V
4.2.0
回答by Neo
in package.json
在package.json 中
"scripts":{
// "start": "node ./bin/www"
"start": "nodemon ./bin/www"
}
the following would now be equivalent:
以下内容现在是等价的:
$ npm start
$ nodemon ./bin/www
回答by swanson_ron
This also works: Include this in your app.js (it does the same thing as the neolivz4ever said)
这也有效:将其包含在您的 app.js 中(它与 neolivz4ever 所说的相同)
app.set('port', process.env.PORT || 3000);
var server = app.listen(app.get('port'), function() {
console.log('Express server listening on port ' + server.address().port);
});
回答by IT Vlogs
you too use define your for nodemon:
你也使用定义你的 nodemon :
$ nodemon --inspect ./bin/www 3000
$ nodemon --inspect ./bin/www 3000
回答by ohkts11
If you are looking for the way to specify the port number with nodemon + express-generator, go to bin/www and change the line
如果您正在寻找使用 nodemon + express-generator 指定端口号的方法,请转到 bin/www 并更改行
var port = normalizePort(process.env.PORT || '3000');
to specific number. For example,
到具体号码。例如,
var port = normalizePort(process.env.PORT || '1234');
回答by Tiago Gouvêa
Additionally, some times, the port are just in use. If the other solutions not work for you, try change the port. It may be in use for some other node instance.
此外,有时,端口只是在使用中。如果其他解决方案不适合您,请尝试更改端口。它可能正在用于其他一些节点实例。

