Asaf Cohen
Asaf Cohen

Reputation: 41

Sails JS API SSL/https support

I have an Ubuntu machine with WordPress installed as my front end. It works with my www.example.com and uses HTTPS solely (if you use HTTP you will be redirected automatically to HTTPS). On the same machine I have sails app server installed (version 0.12) and running on port 1337. I have a form on my WordPress that sends a post commands to an api running on sails app and I'm getting error since the site/page was loaded using HTTPS and the api doesn't.

If I use Chrome to access http://www.example.com:1337/api then api is activated and I get a the result. When I try to use https://www.example.com:1337/api - I get this error:

This site can’t provide a secure connection domain.com sent an invalid response. Try running Windows Network Diagnostics. ERR_SSL_PROTOCOL_ERROR

I have the proper cert files on the machine, what I need to do in order to instruct sails to use HTTPS instead of HTTP?

Thanks!

Upvotes: 0

Views: 696

Answers (1)

Mariusz Wiazowski
Mariusz Wiazowski

Reputation: 2446

I think you should provide a configuration file in order to tell Sails to use these certificates in production. Create a file in config/local.js which contains all the paths for your SSL certificates or copy this code to config/env/production.js:

var fs = require('fs');

/**
 * SSL certificates config file
 */
module.exports = {

   ssl : {
        ca: fs.readFileSync('/etc/letsencrypt/live/example.com/chain.pem'),
        key: fs.readFileSync('/etc/letsencrypt/live/example.com/privkey.pem'),
        cert: fs.readFileSync('/etc/letsencrypt/live/example.com/cert.pem')
   },

   port: process.env.PORT || 443

};

A small tip: if you want to redirect calls from 80 port to 443 port you can use NGINX.

Upvotes: 1

Related Questions