Nodejs + expressjs - pm2并永远不会工作?

时间:2016-07-02 11:58:22

标签: node.js express forever pm2

我想在我的nodejs服务器上永久运行应用程序。但它似乎不适用于pm2forever

如果我手动启动应用程序:

$ npm start

> iot@1.0.0 start /var/www/path/to/myapp
> node ./bin/www

我可以在浏览器上成功查看该应用。

PM2

$ pm2 start /var/www/path/to/myapp/app.js

[PM2] Applying action restartProcessId on app [app](ids: 0)
[PM2] [app](0) ✓
[PM2] Process successfully started
┌──────────┬────┬──────┬───────┬────────┬─────────┬────────┬─────────────┬──────────┐
│ App name │ id │ mode │ pid   │ status │ restart │ uptime │ memory      │ watching │
├──────────┼────┼──────┼───────┼────────┼─────────┼────────┼─────────────┼──────────┤
│ app      │ 0  │ fork │ 29210 │ online │ 0       │ 0s     │ 12.383 MB   │ disabled │
└──────────┴────┴──────┴───────┴────────┴─────────┴────────┴─────────────┴──────────┘

我也试过这个:

$ cd /var/www/path/to/myapp
$ pm2 start app.js

它说应用程序在线。但是,当我尝试在浏览器上访问该应用时:http://127.0.0.1:3000/

我收到此错误:

  

此网页不可用

     

ERR_CONNECTION_REFUSED

永远:

$ sudo forever start app.js
warn:    --minUptime not set. Defaulting to: 1000ms
warn:    --spinSleepTime not set. Your script will exit if it does not stay up for at least 1000ms
info:    Forever processing file: app.js

但是没有运气。

我错过了哪些想法?

修改

在package.json中:

{
  "name": "iot",
  "version": "1.0.0",
  "private": true,
  "scripts": {
    "start": "node ./bin/www"
  },
  "dependencies": {
    "bcrypt": "^0.8.7",
    "body-parser": "~1.15.1",
    "connect-mongo": "^1.2.1",
    "cookie-parser": "~1.4.3",
    "debug": "~2.2.0",
    "express": "~4.14.0",
    "express-session": "^1.13.0",
    "hat": "0.0.3",
    "rand-token": "^0.2.1",
    "method-override": "^2.3.6",
    "mongoose": "^4.5.1",
    "morgan": "~1.7.0",
    "pug": "~2.0.0-beta3",
    "serve-favicon": "~2.3.0"
  }
}

如果我删除"scripts"并运行npm update

{
  "name": "iot",
  "version": "1.0.0",
  "private": true,
  "dependencies": {
    "bcrypt": "^0.8.7",
    "body-parser": "~1.15.1",
    "connect-mongo": "^1.2.1",
    "cookie-parser": "~1.4.3",
    "debug": "~2.2.0",
    "express": "~4.14.0",
    "express-session": "^1.13.0",
    "hat": "0.0.3",
    "rand-token": "^0.2.1",
    "method-override": "^2.3.6",
    "mongoose": "^4.5.1",
    "morgan": "~1.7.0",
    "pug": "~2.0.0-beta3",
    "serve-favicon": "~2.3.0"
  }
}

然后我再次手动启动应用程序,但出现错误:

$ npm start
npm ERR! Linux 3.13.0-76-generic
npm ERR! argv "/usr/local/bin/node" "/usr/local/bin/npm" "start"
npm ERR! node v5.10.1
npm ERR! npm  v3.8.3

npm ERR! missing script: start
npm ERR! 
npm ERR! If you need help, you may report this error at:
npm ERR!     <https://github.com/npm/npm/issues>

1 个答案:

答案 0 :(得分:0)

问题是由 express-generator 引起的,我用它来生成我的应用项目!

它有一个bin文件夹,用于运行http。所以我必须cd bin然后pm2 start www