Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Problem starting a CompoundJS App #58

Closed
leandono opened this issue Jul 22, 2013 · 1 comment
Closed

Problem starting a CompoundJS App #58

leandono opened this issue Jul 22, 2013 · 1 comment

Comments

@leandono
Copy link

Hi,
For some reason, when i want run a app created with CompoundJS with pm2, the aplication doesn't start (but in other ways, like node server.js, using nodemon or forever, the problem doesn't happen).
The script server.js is generic (automatically created by Compound):

#!/usr/bin/env node

/**
 * Server module exports method returning new instance of app.
 *
 * @param {Object} params - compound/express webserver initialization params.
 * @returns CompoundJS powered express webserver
 */
var app = module.exports = function getServerInstance(params) {
    params = params || {};
    // specify current dir as default root of server
    params.root = params.root || __dirname;
    return require('compound').createServer(params);
};

if (!module.parent) {
    var port = process.env.PORT || 3000;
    var host = process.env.HOST || '0.0.0.0';

    var server = app();
    server.listen(port, host, function () {
        console.log(
            'Compound server listening on %s:%d within %s environment',
            host, port, server.set('env')
        );
    });
}

And the console don't show any problem:

$ pm2 start server.js -v
⌬ PM2 Configuration :  { script: 'server.js', name: 'server' }
⌬ PM2 You can write the current configuration by adding -w option
{ script: 'server.js',
  name: 'server',
  pm_exec_path: '<path>/server.js' }
⌬ PM2 Process launched

Any idea? Specifically i don't know if this a problem with compoundjs or with pm2.

Best regards

@Unitech
Copy link
Owner

Unitech commented Jul 29, 2013

Yes it doesn't work because in the server.js CompoundJS use module.parent, and as pm2 wrap the code in child process the code is not executed.

To fix it :

#!/usr/bin/env node

/**
 * Server module exports method returning new instance of app.
 *
 * @param {Object} params - compound/express webserver initialization params.
 * @returns CompoundJS powered express webserver
 */
var app = module.exports = function getServerInstance(params) {
    params = params || {};
    // specify current dir as default root of server
    params.root = params.root || __dirname;
    return require('compound').createServer(params);
};

//if (!module.parent) {
    var port = process.env.PORT || 3000;
    var host = process.env.HOST || '0.0.0.0';

    var server = app();
    server.listen(port, host, function () {
        console.log(
            'Compound server listening on %s:%d within %s environment',
            host, port, server.set('env')
        );
    });
//}

@Unitech Unitech closed this as completed Jul 29, 2013
Unitech added a commit that referenced this issue Sep 16, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants