locomotive.js听取http和https

时间:2013-03-19 10:58:31

标签: javascript node.js locomotivejs

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

我现在有点困惑,有什么帮助吗?

谢谢!

2 个答案:

答案 0 :(得分: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重定向部分),但这些是次要的。

答案 1 :(得分: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);