HTTPS on Express, Sails

Created at 2017-12-20 Updated at 2024-04-17 - 3 min. read

I was trying to figure out how to setup https on a domain. Then i came across letsencrypt. Let’s Encrypt is a free, automated, and open certificate authority (CA), run for the public’s benefit. It is a service provided by the Internet Security Research Group (ISRG).

If your achietechture incluedes one of the following.

  • Apache
  • Nginx
  • Haproxy
  • Plesk

Kindly visit to this url. To get your commands for the installation. This tutorial is for express, sails running on ubuntu 16.04 without the above server.

Install Certbot

Certbot is an easy-to-use automatic client that fetches and deploys SSL/TLS certificates for your webserver. More information can be found here.
sudo apt-get update
sudo apt-get install software-properties-common
sudo add-apt-repository ppa:certbot/certbot
sudo apt-get update
sudo apt-get install certbot
sudo certbot renew --dry-run

Setting up things

Run the below command and fill all the information regarding your domain. Let assume your domail name is abc.com

sudo certbot certonly

As soon as you would complete the process. Your certificates were be generated in
“/etc/letsencrypt/live/abc.com” folder. You can check it by writting following command in your terminal.

cd /etc/letsencrypt/live/abc.com/

If you found the following 3 files then bingo!! you got every step correct.

  • chain.pem
  • privkey.pem
  • cert.pem

Setting up Express || Sails

For express kindly do the following changes in app.js

1
2
3
4
5
6
7
8
9
10
11
12
13
var https = require('https');
var fs = require('fs');

var options = {
key: fs.readFileSync('/etc/letsencrypt/live/abc.com/privkey.pem'),
cert: fs.readFileSync('/etc/letsencrypt/live/abc.com/cert.pem'),
ca: fs.readFileSync('/etc/letsencrypt/live/abc.com/chain.pem')
};

https.createServer(options, function (req, res) {
res.writeHead(200);
res.end("hello world\n");
}).listen(8000);

For sails. Kindly do the following changes in config/env/production.js

1
2
3
4
5
ssl: {
ca: require('fs').readFileSync('/etc/letsencrypt/live/abc.com/chain.pem')
key: require('fs').readFileSync('/etc/letsencrypt/live/abc.com/privkey.pem'),
cert: require('fs').readFileSync('/etc/letsencrypt/live/abc.com/cert.pem')
}

After this change the port address to 443.
Now as soon as you will start the server you would be able to see that server is lifted on https://localhost.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
    $ sails lift

Sails.js <|
v0.12.14 |\
/|.\
/ || \
,' |' \
.-'.-==|/_--'
`--'-------'
__---___--___---___--___---___--___
____---___--___---___--___---___--___-__


Server lifted in /home/ashu/dev/node/sails/testProject
To see your app, visit https://localhost
To shut down Sails, press <CTRL> + C at any time.
-------------------------------------------------------
:: Wed Dec 20 2017 14:04:21 GMT+0000 (UTC)
Environment : production
Port : 443
-------------------------------------------------------

Bingo.. Your site has now turned into https. Enjoy :)

Site by Ashutosh Kumar Singh using Hexo & Random

Traveller - Developer - Foodie - Biker - Cyclist - Rider - Technocrat