0

在某些情况下,我需要在我的网站上使用 SSL,

我已按照自述文件中的示例进行操作

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

我的 server.js 是

#!/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')
        );
    });
}

现在的问题是我无法获得http,只有https。如果我只是将我的证书和密钥放在配置文件夹中并拥有

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

然后我得到一些错误

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

让 Compoundjs 支持 https 和 http 路由的最快和最简单的方法是什么,我是否需要有两台服务器(就像你经常使用直接快递一样)?

4

1 回答 1

0

是的 - 需要像使用 express 一样拥有两台服务器。所以

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')
        );
    });
 }
于 2015-08-19T07:34:56.160 回答