admin管理员组

文章数量:1181439

I am trying to figure out how to lift a sails app that responds to both HTTP and HTTPS requests. I used the config/local.js method of configuring express like so (detailed here):

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

};

However, while this works, it results in the server only being able to serve HTTPS requests. Has anyone figured out how to get this done, without creating a separate HTTP only server that redirects to port 443 ?

Thanks

I am trying to figure out how to lift a sails app that responds to both HTTP and HTTPS requests. I used the config/local.js method of configuring express like so (detailed here):

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

};

However, while this works, it results in the server only being able to serve HTTPS requests. Has anyone figured out how to get this done, without creating a separate HTTP only server that redirects to port 443 ?

Thanks

Share Improve this question asked Feb 6, 2014 at 19:57 MOneSixInCodeMOneSixInCode 2462 silver badges6 bronze badges 4
  • question: is your end goal to redirect http --> https? In otherwords if someone types example.com they get redirected to example.com. If that is the case you can do this a few different ways outside of sailsjs. – NDBoost Commented Feb 17, 2014 at 17:11
  • I ultimately just used nginx on the server and created a redirect rule for HTTP->HTTPS. – MOneSixInCode Commented Feb 18, 2014 at 18:12
  • thats what i was going to suggest as nginx is probably the preferred method of serving a sailsjs app in production.. – NDBoost Commented Feb 18, 2014 at 20:10
  • jsbot.io/node/http-and-https-handle-with-sailsjs This may be helpful – Nishchit Commented May 1, 2016 at 11:31
Add a comment  | 

8 Answers 8

Reset to default 6

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 here
if(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 );
    // ...
};

I've been wondering for running sailsjs on HTTPS and after going back and forth here is the solution working for me.

  1. I copied .crt and .key file in project directory under ssl folder
  2. config/local.js updated with following as I'm using sailsjs 0.12.3

    
    ssl: {
      key: require('fs').readFileSync(require('path').resolve(__dirname + '/ssl/mykey.key')),
      cert: require('fs').readFileSync(require('path').resolve(__dirname + '/ssl/mycert.crt'))
    }
    
    
    

Check this out: https://github.com/balderdashy/sails/issues/862

module.exports.bootstrap = function (cb) {
    var express = require("express"),
         app = express();

    app.get('*', function(req,res) {  
        res.redirect('https://' + req.headers.host + req.url)
    }).listen(80);

    cb();
};

If you are using sails 0.10.x you may need to move this to module.exports.express (config/express.js) I'm not sure...

As an update from 0.9 to 0.10 , the local.js file should now have

ssl : {
key: fs.readFileSync(‘server.key’),
cert: fs.readFileSync(‘server.crt’)
}

instead of

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

Doesnt look like there is a way to do this within sails at the moment so I ultimately just used nginx on the server and created a redirect rule for HTTP->HTTPS.

You must take care of SSL & HTTPS , If both use same port by proxy.

var express = require("express"),
     app = express();

app.get('*', function(req,res) {  

    if(req.isSocket){           
        return res.redirect('wss://' + req.headers.host + req.url)  
    }
    else{
        return res.redirect('https://' + req.headers.host + req.url)    
    }

}).listen(80);

本文标签: javascriptSailsJS HTTPHTTPSStack Overflow