Sails.JS HTTP + HTTPS Sails.JS HTTP + HTTPS express express

Sails.JS HTTP + HTTPS


1-Uncomment the path to your SSL Certificates in your local.js or add path to your SSL Certificates in your config/env/production.js.

    module.exports  = { ssl: {     ca: require('fs').readFileSync(__dirname + '/ssl/ca.crt'),     key: require('fs').readFileSync(__dirname + '/ssl/key.key'),     cert: require('fs').readFileSync(__dirname + '/ssl/cert.crt')   }, port: 443}

2-Add a policies section in your config/env/production.js

    module.exports  = { ssl: {     ca: require('fs').readFileSync(__dirname + '/ssl/ca.crt'),     key: require('fs').readFileSync(__dirname + '/ssl/key.key'),     cert: require('fs').readFileSync(__dirname + '/ssl/cert.crt')   }, port: 443, policies: {    '*': 'isHTTPS'   }}

3-Create a isHTTPS.js policy in your api/policies folder. This policy will redirect the HTTP request to HTTPS

  module.exports = function(req, res, next) {    if (req.secure) {        // Already https; don't do anything special.        next();    } else {        // Redirect to https.        res.redirect('https://' + req.headers.host + req.url);    }};

4-Then we will edit the config/bootstrap.js file and listen to port 80 if the environment is production, so that we can redirect the requests to 443 i.e SSL

    var http = require( 'http' );module.exports.bootstrap = function(cb) {    //If the environment is production, then listen on port 80     if(sails.config.environment === "production") {        http.createServer( sails.hooks.http.app ).listen( 80 );            }    cb();}


I was able to get http and https running with sails by modifying my configs/local.js file:

var fs = require('fs');var local = ... // Your default local.js settings should go hereif(require('optimist').argv.https) {    local.express = {        serverOptions : {            key: fs.readFileSync('ssl/server.key'),            cert: fs.readFileSync('ssl/server.crt')        }    };    local.port = 1338; // This port should be different than your default port}module.exports = local;

Now you will need to run your sails app twice. The first time type in your regular sails lift command. The second time run sails lift --https

This should allow you to have both http and https servers running off of the same codebase. As some other people have mentioned, nginx is a much better solution for handling this sort of this, but if you are doing local development and don't want to have to install nginx the solution above is just fine.


To have both HTTP & HTTPS, avoiding the redirection way:

Configure your SSL key & cert and HTTPS port in your /config/env/production.js:

var fs = require( 'fs' );module.exports = {    port: 443,    ssl: {        key: fs.readFileSync( 'ssl_key.key' ),        cert: fs.readFileSync( 'ssl_key.crt' )    }    //, ...};

Then, listen port 80 with a second HTTP server into /config/bootstrap.js:

var http = require( 'http' );module.exports.bootstrap = function ( cb ) {    // ...    if ( process.env.NODE_ENV == 'production' )        http.createServer( sails.hooks.http.app ).listen( 80 );    // ...};