我需要在某些情况下在我的网站上使用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路由的最快捷最简单的方法是什么?我是否需要有两台服务器(就像你经常使用直接快递一样)?
答案 0 :(得分:0)
是 - 需要像快递那样拥有两台服务器。所以
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')
);
});
}