gpt4 book ai didi

javascript - locomotive.js 监听http和https

转载 作者:太空宇宙 更新时间:2023-11-04 01:12:25 24 4
gpt4 key购买 nike

我有一个 locomotive.js MVC 项目,它在 http 上监听。我想监听 https ,并将所有 http 重定向到 https。

我找不到node.js的createServer,我找到的唯一代码是:在/lib/node_modules/locomotive/lib/locomotive/cli/server.js下

console.log('booting app at %s in %s environment', dir, env);

locomotive.boot(dir, env, function(err, server) {
if (err) {
throw err;
}

server.listen(port, address, function() {
var addr = this.address();
console.log('listening on %s:%d', addr.address, addr.port, addr);
});
});

更改为:

console.log('booting app at %s in %s environment', dir, env);
var crypto = require('crypto'),
fs = require("fs");
var privateKey = fs.readFileSync('/privatekey.pem').toString();
var certificate = fs.readFileSync('/certificate.pem').toString();
var https = require('https');
var credentials = crypto.createCredentials({key: privateKey, cert: certificate});

我现在有点卡住了,有什么帮助吗?

谢谢!

最佳答案

这还没有经过很好的测试,但它似乎有效:

  // start.js
var locomotive = require('locomotive');
var app = new locomotive.Locomotive();
var http = require('http');
var https = require('https');
var fs = require('fs');

app.boot(__dirname, 'development', function(err, server) {
var options = {
key : fs.readFileSync('server.key'),
cert: fs.readFileSync('server.crt')
};
https.createServer(options, server).listen(port, address);
});

它有几个问题(硬编码的目录名称和环境,并且它不包括 HTTP 重定向部分),但这些都是次要的。

关于javascript - locomotive.js 监听http和https,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15497570/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com