Search code examples
sslexpresscompoundjs

compoundjs support both ssl and normal http


I needed to use SSL on my site in certain situations,

I have followed the example in the readme

require('compound').createServer({
    key: fs.readFileSync('/tmp/tls.key').toString(),
    cert: fs.readFileSync('/tmp/tls.cert').toString()
});

My server.js is

#!/usr/bin/env node
var fs = require('fs');
/**
 * 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;
    params["key"] = fs.readFileSync('/tmp/tls.key').toString();
    params["cert"] =  fs.readFileSync('/tmp/tls.cert').toString();
    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')
        );
    });
}

Now the problem is I can't get http, only https. If I just put my cert and key inside of the config folder and have

**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);
};**

Then I get some error that

config/tls.cert:1
tion (exports, require, module, __filename, __dirname) { -----BEGIN CERTIFICAT
                                                                    ^^^^^^^^^^^
SyntaxError: Unexpected identifier
    at Module._compile (module.js:439:25)

What is the quickest and easiest way to get compoundjs to support both https and http routes, do I need to have two servers (like you would often with straight express)?


Solution

  • YES - need to have two servers like you do with express. So

    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);
    };
    
    
    var port = process.env.PORT || 80;
    var host = process.env.HOST || '0.0.0.0';
    var port_https = 443;
    
    var server = app();
    
    
    server.listen(port, host, function () {
        server.set("confvar", process.env.SERVER_NAME);
        console.log(
            'Compound server listening on %s:%d within %s environment',
            host, port, server.set('env')
        );
    });
    
     if(fs.existsSync(PATH TO KEY GOES HERE)){
        var apphttps = module.exports = function getServerInstance(params) {
            params = params || {};
            // specify current dir as default root of server
            params.root = params.root || __dirname;    
            params.key = fs.readFileSync(PATH TO KEY GOES HERE).toString();
            params.cert = fs.readFileSync(PATH TO CERT GOES HERE).toString();
            return require('compound').createServer(params);
        };
        var serverhttps = apphttps();
    
        serverhttps.listen(port_https, host, function () {
            serverhttps.set("confvar", process.env.SERVER_NAME);
            console.log(
                'Compound https server listening on %s:%d within %s environment',
                host, port_https, serverhttps.set('env')
            );
        });
     }