1
votes

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

> [email protected] 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>
1
What is the body of scripts.start in package.json? - Nonemoticoner
@Nonemoticoner Please see my edit above. thanks. What is scripts.start for? - Run
I meant start script. Sorry for oversimplyfying it. Hmm, you start node for a directory but when using forever and pm2 you start from particular file. Dunno, but maybe that's a cause. - Nonemoticoner
Basically you could remove it as scripts are optional. It is only to help the user of your app to easiely run an app i.e. type npm start instead of node ./bin/www. Also, it runs as batch script as far I remember. - Nonemoticoner
Back to your issue, it might be some directory confusion (at least that's my only idea about that). I faced smth like that once when attempting to use forever from directory where the main script was placed. You should try running from parent directory i.e. where package.json is. - Nonemoticoner

1 Answers

1
votes

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.