Sails.JS: Harnessing the Power of Both HTTP and HTTPS

I am currently exploring the process of setting up a sails app that can handle both HTTP and HTTPS requests. To achieve this, I followed the steps outlined in the blog post available 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, I encountered an issue where the configuration only allowed the server to respond to HTTPS requests. I am seeking advice or solutions from individuals who have successfully implemented a setup that caters to both HTTP and HTTPS without needing a separate HTTP-only server with redirects to port 443.

Any insights would be greatly appreciated.

Answer №1

Update your local.js file or production.js file to include the path to your SSL Certificates.

    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
}

Include a policies section in your production environment configuration file.

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

Create a custom policy named isHTTPS.js in your api/policies folder to redirect HTTP requests to HTTPS.

  module.exports = function(req, res, next) {
    if (req.secure) {
        // Already using HTTPS; no action needed.
        next();
    } else {
        // Redirect to HTTPS.
        res.redirect('https://' + req.headers.host + req.url);
    }
};

In the config/bootstrap.js file, make changes to listen on port 80 for production environment and redirect requests to port 443 (SSL).

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

Answer №2

To enable both http and https on Sails, I customized my configs/local.js file as follows:

var fs = require('fs');
var localSettings = ... // Add your default local.js settings here
if(require('optimist').argv.useHTTPS) {
    localSettings.express = {
        serverOptions : {
            key: fs.readFileSync('ssl/server.key'),
            cert: fs.readFileSync('ssl/server.crt')
        }
    };
    localSettings.port = 1338; // Use a different port than the default
}
module.exports = localSettings;

You will need to lift your Sails app twice. First run sails lift, then run sails lift --https.

This setup allows you to have both http and https servers running from the same codebase. While nginx is recommended for production environments, this workaround is suitable for local development without needing to install nginx.

Answer №3

For a setup with both HTTP and HTTPS without redirection:

To enable both HTTP and HTTPS, configure your SSL key, certificate, and HTTPS port in the /config/env/production.js file:

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

Next, set up a second HTTP server to listen on port 80 in the /config/bootstrap.js file:

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

Answer №4

After much contemplation, I finally found the perfect solution for running sailsjs on HTTPS.

  1. To enable HTTPS, I placed the .crt and .key files into the ssl folder within my project directory.
  2. I made adjustments to config/local.js to work with sailsjs 0.12.3 as shown below:

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

Answer №5

Take a look at this: 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();
};

For those using sails version 0.10.x, it might be necessary to transfer this code to module.exports.express (config/express.js). But, there's no certainty...

Answer №6

When updating from version 0.9 to 0.10, it is important to note that the local.js file now requires the following setup:

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

This replaces the previous configuration in local.js:

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

Answer №7

It appears that implementing this feature directly in sails is not possible currently. Therefore, I decided to utilize nginx on the server and set up a redirect rule for HTTP to HTTPS.

Answer №8

It is crucial to manage both SSL and HTTPS when they share the same port through a proxy.

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

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Error-free saving issue with Mongoose model in Node.js

While developing a simple Node.js Express API, I encountered an unusual issue. I created a model, inserted data into it, and attempted to save it to my MongoDB database. However, the record was not saved, and no errors were reported. I thoroughly checked f ...

"Encountering difficulty in retrieving information from $q and integrating it into the

I am facing an issue with binding data from an API to my scope using promises in AngularJS. Despite successfully retrieving the JSON data from the server, the $scope variable remains empty. Any assistance on this matter would be greatly appreciated. Thank ...

Changing an image depending on the visibility of another image can be accomplished by using JavaScript to

On a webpage, I have a logo that I want to change based on the visibility of another image. The other image is part of a list that fades between images and repeats. The goal is for Logo1 to display when a specific image is shown, and then switch back to L ...

"Encountered a syntax error while attempting to reference an attribute on an empty object

> "[object Number]" === Object.prototype.toString.call(1) // #1 < true > "[object Number]" === {}.toString.call(1) // #2 < true > {}.toString.call(1) === "[object Number]" // #3 < SyntaxError: Unexpected token ...

What is the process of transforming an array object into an object containing the same key

I have a collection of objects, each containing the same field called roleMapping. My goal is to group all elements with the same key into an array. Below is the object collection I need to convert: a = [ { roleMapping: { 123: { shortNa ...

Unable to trigger @click event on nuxt-link component within Nuxt 3

As per a Nuxt 2 question on Stack Overflow, it was suggested that the following code should prevent nuxt-link from navigating to a new page: <nuxt-link :to="`/meet`" class="group font-normal" @click.native="event => event.pre ...

Difference Between For Loop and Map Function

I encountered two scenarios while trying to understand what was happening, but the clarity eluded me. I came across information suggesting that when running async calls in a for loop or map, Promise.all must be used. Let me share my experiences: Initially ...

Encountering a TypeError when attempting to pass the onChange function as props to Grandchildren due to 'this' being undefined

Struggling to pass an onChange function from parent to grandchild component and encountering an error. TypeError: this is undefined The code snippet causing the issue: const TaskTable = React.createClass({ getInitialState: function() { return {dat ...

Contrast the differences between arrays and inserting data into specific index positions

In this scenario, I have two arrays structured as follows: arr1=[{room_no:1,bed_no:'1A'}, {room_no:1,bed_no:'1B'}, {room_no:2,bed_no:'2A'}, {room_no:3,bed_no:'3A'}, {room_no:3,bed_no:'3B ...

Gathering information from various collections in MongoDB to determine the population of mongoose

I am currently working on establishing a connection in my API between Retailers and GeofencePoints. The goal is for a retailer object to contain a list of geofence points. I have been referring to the official documentation at: http://mongoosejs.com/docs/p ...

When querying with pgAdmin, values are displayed while Express JS may return null for a query result

I am facing an issue with the code snippet below that is meant to query a list of country names: router.get('/', function(req, res) { console.log("/"); const query = `SELECT DISTINCT name FROM countries`; db.map(query, [], a ...

How can I convert the date format from ngbDatepicker to a string in the onSubmit() function of a form

I'm facing an issue with converting the date format from ngbDatepicker to a string before sending the data to my backend API. The API only accepts dates in string format, so I attempted to convert it using submittedData.MaturityDate.toString(); and su ...

Angular.js testing for inject() function runs before the execution of a run phase code block

I am facing a situation where I need to load HTML5 Audio specifically for mobile devices, which requires user interaction like ontouchstart. To achieve this, I have implemented the necessary logic in an Angular run phase to ensure it is executed at the ear ...

"How can we trigger a re-render of a React component once a promise is fulfilled? Is there a way to prevent rendering until the

Having attempted to make updates to a functional component that interfaces with an azure-devops-ui/Filter, I've encountered a situation where I am utilizing the azure-devops-extension-sdk to handle async responses. This component is intended to be use ...

Issue with Camera inversion not functioning properly in THREE.js with 1 Renderer and 2 Viewports

Check out this JSFiddle example In my project, I have a single scene with two cameras. Each camera is assigned to its viewport, and both viewports are placed side by side on the same renderer object. My goal is to have the second camera display a mirrore ...

How can I modify an array in Couchbase by using an N1QL query?

{ "name":"sara", "emailId":"<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="abcde8e2ea9627225c4b9a3afa7bbcc808cb554a1adaf">[email protected]</a>", "subjects" : [{ "name":"Math", "tutor":"alice", "classes" ...

Error: Order placement failed due to a validation error

I've been working on developing an ecommerce application, and while the frontend is complete, I've encountered a problem when placing an order. After going through all my files, I couldn't pinpoint where the issue was originating from. Despi ...

Tips for ensuring only digits can be entered in a textbox on Firefox

Having trouble validating digits in a textbox on both IE 9 and Firefox versions 4 & 5. Does anyone know how to solve this issue? I've attempted the solutions provided in previous questions, but I'm still encountering problems. My goal is to only ...

Form appears outside the modal window

I am facing an issue with my modal where the form inside is displaying outside of the modal itself. Despite trying to adjust the CSS display settings and switching to react-bootstrap from regular bootstrap, the problem persists. I am uncertain about what s ...

Tips for finding the *index* of distinct values in an array

My goal is to identify the indexes of unique values in an array. While I've come across various methods for removing duplicate values from an array, such as those found here: How to get unique values in an array I'm currently struggling to find ...