From 13eafcc31e1cf8e872810587aebbe426c61bccfd Mon Sep 17 00:00:00 2001 From: Sam Roberts Date: Tue, 7 Jan 2014 10:32:00 -0800 Subject: [PATCH] Allow app to be runnable within a supervisor app.js is designed to be the package.main, so app can be required. If its run as the main module (`node app.js`), then it also starts up. Those features are desired, but the conditionality of startup in app.js means it is difficult to start the app when run under a supervisor such as strongloop/strong-supervisor or unitech/pm2. To fix: - Create a single line server.js which unconditionally starts - Add a .bin property to package.json so the app can be globally installed. - Exports the startup code as a function(`require('app.js').main` when app is being used as a module (so behaviour of `node server.js` is identical to `node app.js`). --- templates/app.js | 8 ++++++-- templates/package.js | 1 + templates/server.js | 3 +++ 3 files changed, 10 insertions(+), 2 deletions(-) create mode 100755 templates/server.js diff --git a/templates/app.js b/templates/app.js index c8637a1c..f7eff0e9 100644 --- a/templates/app.js +++ b/templates/app.js @@ -135,8 +135,8 @@ app.enableAuth(); * (only if this module is the main module) */ -if(require.main === module) { - require('http').createServer(app).listen(app.get('port'), app.get('host'), +app.start = function() { + return require('http').createServer(app).listen(app.get('port'), app.get('host'), function(){ var baseUrl = 'http://' + app.get('host') + ':' + app.get('port'); if (explorerConfigured) { @@ -150,3 +150,7 @@ if(require.main === module) { } ); } + +if(require.main === module) { + app.start(); +} diff --git a/templates/package.js b/templates/package.js index b05a7df9..4c7275fa 100644 --- a/templates/package.js +++ b/templates/package.js @@ -1,6 +1,7 @@ module.exports = { "version": "0.0.0", "main": "app.js", + "bin": "server.js", "scripts": { "start": "node app.js" }, diff --git a/templates/server.js b/templates/server.js new file mode 100755 index 00000000..0287d506 --- /dev/null +++ b/templates/server.js @@ -0,0 +1,3 @@ +#!/usr/bin/env node + +require('./').start();