how to configure https in sails.js

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?

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

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

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'),
}}

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: http://stackoverflow.com/a/28565113/2459071

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