简体   繁体   中英

Nodejs + expressjs - pm2 and forever don't work?

I want to make an app running forever on my nodejs server. But it does not seem to work with pm2 and forever .

If I start the app manually:

$ npm start

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

I can see the app on my browser successfully.

But with 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 │
└──────────┴────┴──────┴───────┴────────┴─────────┴────────┴─────────────┴──────────┘

I tried with this too:

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

It says that the app is online. But when I try to access the app on my browser: http://127.0.0.1:3000/

I get this error:

This webpage is not available

ERR_CONNECTION_REFUSED

With forever:

$ 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

But no luck at all.

Any ideas what I have missed?

EDIT:

In 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"
  }
}

If I remove "scripts" and run 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"
  }
}

Then I start the app manually again but get an error:

$ 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>

The problem is caused by express-generator which i use it to generate my app project!

It has a bin folder in which it runs the http. so I have to cd bin then pm2 start www .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM