Supervisor node.js "Node program error exiting with code 0"

When I install the express pads application

express 

Then run npm install

 npm install 

and then run the supervisor

 supervisor app 

I get

 Starting child process with 'node app' Program node app exited with code 0 

The app.js file is the primary instance of the default expression.

 var express = require('express'); var path = require('path'); var favicon = require('static-favicon'); var logger = require('morgan'); var cookieParser = require('cookie-parser'); var bodyParser = require('body-parser'); var routes = require('./routes/index'); var users = require('./routes/users'); var app = express(); // view engine setup app.set('views', path.join(__dirname, 'views')); app.set('view engine', 'jade'); app.use(favicon()); app.use(logger('dev')); app.use(bodyParser.json()); app.use(bodyParser.urlencoded()); app.use(cookieParser()); app.use(express.static(path.join(__dirname, 'public'))); app.use('/', routes); app.use('/users', users); /// catch 404 and forward to error handler app.use(function(req, res, next) { var err = new Error('Not Found'); err.status = 404; next(err); }); /// error handlers // development error handler // will print stacktrace if (app.get('env') === 'development') { app.use(function(err, req, res, next) { res.status(err.status || 500); res.render('error', { message: err.message, error: err }); }); } // production error handler // no stacktraces leaked to user app.use(function(err, req, res, next) { res.status(err.status || 500); res.render('error', { message: err.message, error: {} }); }); module.exports = app; 
+7
express supervisor
source share
3 answers

The application that the generator makes calls to ./bin/www , which includes app.js , and then starts listening to traffic.

app.js does not do this itself .

I think this is important to understand.

app.listen not called in app.js , but it is called in ./bin/www ... and that is why you get the result exit 0 . When you call app.js , not ./bin/www , it starts through the file, but since the command does not listen to traffic, the program ends normally ... i.e. having done nothing.

However, you have two options.

Option 1

If you have a ./bin/www file, you can run supervisor ./bin/www to get started.

Option 2

If you don’t have a ./bin/www file for any reason, you can edit the application file to look like this.

In the application list, replace

 module.exports = app; 

with this

 app.set('port', process.env.PORT || 3000); var server = app.listen(app.get('port'), function() { debug('Express server listening on port ' + server.address().port); }); 

Important Note

While this editing will start listening to the application and you will no longer get exit 0 , I can not guarantee that the application will not crash with any other error if other files and directories are missing. For example, if the routes directory is missing, then ads requiring routes/index and routes/users will fail and other bad things will happen.

+18
source share

By default, to run the application, when you use the express generator , you need to insert

DEBUG={appName}:* npm start

{appName} - perhaps this is the name of your folder (as for me), in any case, you can find it in your package.json β†’ "name" ;

0
source share

Thanks for the best answer, finally this works for me:

 "main": "./bin/www", "scripts": { "start": "node ./bin/www" } 
-2
source share

All Articles