HTTPS on Express, Sails
Created at 2017-12-20 Updated at 2024-11-07 - 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.js1
2
3
4
5
6
7
8
9
10
11
12
13var 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 | ssl: { |
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 | $ sails lift |
Bingo.. Your site has now turned into https. Enjoy :)