【问题标题】:nodemon + express, listen port =?nodemon + express,监听端口=?
【发布时间】:2014-07-02 02:12:12
【问题描述】:

我使用 express 创建了一个简单的节点项目:

npm install -g express-generator
express test
cd test/ && npm install
PORT=3000 npm start

这样就可以在端口 3000 上启动并运行测试应用程序。太好了。现在我想使用nodemon 来运行这个项目。我已经安装了:

npm install -g nodemon

在 gihub README 中,它的运行方式与 node 相同。这有点令人困惑,因为新的启动节点的方式是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

但是当我尝试连接时,那里什么都没有。我通过以下方式确认了这一点:

lsof -i TCP:3000

什么也没返回。通常(使用npm start)它会返回:

COMMAND   PID USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
node    18746 user   10u  IPv4 433546      0t0  TCP *:3000 (LISTEN)

谁能告诉这里有什么问题? 如何让应用程序使用nodemon 监听指定端口?

我的设置:

npm -v
1.3.21
node -v
v0.10.24
nodemon -v
v1.0.18
express -V
4.2.0

【问题讨论】:

    标签: node.js express nodemon


    【解决方案1】:

    package.json

      "scripts":{
        // "start": "node ./bin/www"
        "start": "nodemon ./bin/www"
       }
    

    以下内容现在是等价的:

    $ npm start
    $ nodemon ./bin/www
    

    【讨论】:

    • 优秀。这个答案正是我想要的。谢谢!
    • 我没看懂,这怎么为nodemon指定端口?我尝试了这个,但我仍然面临同样的问题 - 没有监听端口 3000
    • 这个不改监听端口,比纯nodejs只用nodemon启动项目
    【解决方案2】:

    这也有效:将其包含在您的 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);
    });
    

    【讨论】:

      【解决方案3】:

      你也使用为 nodemon 定义你的:

      $ nodemon --inspect ./bin/www 3000

      【讨论】:

      • 'node --debug` 和 node --debug-brk 现在已弃用。请改用node --inspectnode --inspect-brk
      【解决方案4】:

      如果您正在寻找使用 nodemon + express-generator 指定端口号的方法,请转到 bin/www 并更改行

      var port = normalizePort(process.env.PORT || '3000');
      

      到特定的数字。例如,

      var port = normalizePort(process.env.PORT || '1234');
      

      【讨论】:

        【解决方案5】:

        此外,有时,该端口只是在使用中。如果其他解决方案不适合您,请尝试更改端口。它可能正在用于其他一些节点实例。

        【讨论】:

          猜你喜欢
          • 2018-10-29
          • 1970-01-01
          • 1970-01-01
          • 2020-06-08
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-07-12
          • 2012-03-18
          相关资源
          最近更新 更多