如何在sails.js中配置https

los*_*hip 14 node.js sails.js

我正在尝试设置本地HTTPS服务器以在Sails.js中进行测试?我无法在sails.js中找到任何指针如何做到这一点?快递,

var express = require('express');
var https = require('https');
var http = require('http');
var fs = require('fs');

// This line is from the Node.js HTTPS documentation.
var options = {
  key: fs.readFileSync('test/fixtures/keys/agent2-key.pem'),
  cert: fs.readFileSync('test/fixtures/keys/agent2-cert.pem')
};

// Create a service (the app object is just a callback).
var app = express();

// Create an HTTP service.
http.createServer(app).listen(80);
// Create an HTTPS service identical to the HTTP service.
https.createServer(options, app).listen(443);
Run Code Online (Sandbox Code Playgroud)

关于sails.js的任何想法?

小智 12

对于sails.js版本0.10,请在config/locals.js中包含此内容(如果locals.js不存在,请创建它):

var fs = require('fs');

module.exports = {

    ssl : {
        key: fs.readFileSync('path-to-key.key'),
       cert: fs.readFileSync('path-to-crt.crt')
    }

};
Run Code Online (Sandbox Code Playgroud)

资料来源:https://stackoverflow.com/a/28565113/2459071


Kel*_*ins 4

如果您使用的是最新的 v0.9(可能还有 v0.8 的某些版本),请查看 config/bootstrap.js 内部。您应该能够通过 sails.express 上下文访问您的 Express 应用程序。从那里我认为你应该能够用它做你想做的事......

#sailsjs irc 频道中也有人说这对他们有用

module.exports.bootstrap = function (cb) {
    var fs = require('fs');
    sails.config.express.serverOptions = {
        key: fs.readFileSync('ssl/key.pem'),
        cert: fs.readFileSync('ssl/cert.pem')
    };
    cb();
};
Run Code Online (Sandbox Code Playgroud)