1

我有一个 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});

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

谢谢 !

4

2 回答 2

1

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

  // 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 重定向部分),但这些都是次要的。

于 2013-03-19T11:16:05.743 回答
0

如果有人看到这个问题,我会在这里发布我的答案:

var fs = require("fs");

var options = {
  key: fs.readFileSync('/privatekey.pem'),
  cert: fs.readFileSync('/certificate.pem')
};

var server = https.createServer(options, this);
return server.listen.apply(server, arguments);
于 2013-03-19T11:15:37.523 回答