how to configure https in sails.js

こ雲淡風輕ζ 提交于 2019-11-29 17:18:21

问题


I am trying to setup a local HTTPS server for testing in Sails.js? I am not able to find any pointer how to do that in sails.js? For express,

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);

Any idea about sails.js?


回答1:


For sails.js version 0.10, include this in config/locals.js (if locals.js does not exist, create it):

var fs = require('fs');

module.exports = {

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

};

Source: https://stackoverflow.com/a/28565113/2459071




回答2:


If you're using the latest v0.9 (and maybe some versions of v0.8) take look inside of config/bootstrap.js. You should be able to access your express app via the sails.express context. From there I think you should be able to do with it what you want to...

Also someone in the #sailsjs irc channel said this worked for them

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();
};



回答3:


Maybe it's just me but I could get either of the above working for sails v0.9.7, but I did get it working by editing the config/local.js file like so;

var fs = require('fs');

module.exports = {   
  port: process.env.PORT || 1337,
  environment: process.env.NODE_ENV || 'development',

  express: { serverOptions : {
    key: fs.readFileSync('ssl/key.pem'),
    cert: fs.readFileSync('ssl/cert.pem')
    }
  }
};

Now I'm not saying this is the 'correct' way to do this, however it works for me!

Shameless self promotion More about this on my blog! End shameless self promotion :D




回答4:


This contribution enhances the solution for to support native mobile applications and old browsers.

This solution worked really well for me when when just using a modern web browser to access my SSL site. However when I attempted to make requests using the AFNetworking library it did not recognise the SSL certificate. This was due to the iPhone application requiring the intermediate SSL certificates (sometimes called the ca bundle).

You can add the intermediate certificate in using the following code.

express: {
  serverOptions : {
    key: fs.readFileSync('ssl/key.pem'),
    cert: fs.readFileSync('ssl/cert.pem'),
    ca: fs.readFileSync('ssl/intermediate.pem')
  }
}

When creating you intermediate certificate (which can normally be downloaded from your SSL certificate provider) it is important to get the order of certificates right. This linux command really helped with debugging.

openssl s_client -connect yoursite.com:443 -showcerts




回答5:


The above does not work for sails v0.9.3. I ended up with the following workaround. (require fs first of course)

express :  {serverOptions : {
    key: fs.readFileSync('ssl/server-key.pem'),
    cert: fs.readFileSync('ssl/server-cert.pem'),
}}



回答6:


I have also faced this kind of issues in my production sails app (v0.11.x and v0.12.x). Android release version apk is not able to connect to sails app and some old version browsers do not accept SSL certificate with web app.

I got some intermediate certificate error like below

The certificate is not trusted in all web browsers. You may need to install an Intermediate/chain certificate to link it to a trusted root certificate.

Finally, I found a solution

ssl: {
      ca: require('fs').readFileSync('ssl/intermediate.crt', 'utf8').toString(),
      key: require('fs').readFileSync('ssl/example_com.key', 'utf8').toString(),
      cert: require('fs').readFileSync('ssl/main.crt', 'utf8').toString()
}

I hope this will help someone.



来源:https://stackoverflow.com/questions/17755916/how-to-configure-https-in-sails-js

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!