Node.js : How to get a basic HTTPS web server up and running!

Rajesh Kumar Yadav - May 9 '21 - - Dev Community

Once you have node.js installed on your system, you can just follow the procedure below to get a basic web server running with support for both HTTP and HTTPS!

Step 1 : Build a Certificate Authority

1.1. create the folder where you want to store your key & certificate :

mkdir conf
Enter fullscreen mode Exit fullscreen mode

1.2. go to that directory :

cd conf
Enter fullscreen mode Exit fullscreen mode

1.3. grab this ca.cnf file to use as a configuration shortcut :

wget https://raw.githubusercontent.com/anders94/https-authorized-clients/master/keys/ca.cnf
Enter fullscreen mode Exit fullscreen mode

1.4. create a new certificate authority using this configuration :

openssl req -new -x509 -days 9999 -config ca.cnf -keyout ca-key.pem -out ca-cert.pem
Enter fullscreen mode Exit fullscreen mode

1.5. now that we have our certificate authority in ca-key.pem and ca-cert.pem, let's generate a private key for the server :

openssl genrsa -out key.pem 4096
Enter fullscreen mode Exit fullscreen mode

1.6. grab this server.cnf file to use as a configuration shortcut:

wget https://raw.githubusercontent.com/anders94/https-authorized-clients/master/keys/server.cnf
Enter fullscreen mode Exit fullscreen mode

1.7. generate the certificate signing request using this configuration :

openssl req -new -config server.cnf -key key.pem -out csr.pem
Enter fullscreen mode Exit fullscreen mode

1.8. sign the request :

openssl x509 -req -extfile server.cnf -days 999 -passin "pass:password" -in csr.pem -CA cacert.pem -CAkey ca-key.pem -CAcreateserial -out cert.pem
Enter fullscreen mode Exit fullscreen mode

Step 2 : Install your certificate as a root certificate

2.1. copy your certificate to your root certificates' folder :

sudo cp ca-crt.pem /usr/local/share/ca-certificates/ca-crt.pem
Enter fullscreen mode Exit fullscreen mode

2.2. update CA store :

sudo update-ca-certificates
Enter fullscreen mode Exit fullscreen mode

Step 3 : Starting your node server

First, you want to create a server.js file that contains your actual server code. The minimal setup for an HTTPS server in Node.js would be something like this :

var https = require('https');
var fs = require('fs');
var httpsOptions = {
 key: fs.readFileSync('path/to/server-key.pem'),
 cert: fs.readFileSync('path/to/server-crt.pem')
};
var app = function (req, res) {
 res.writeHead(200);
 res.end("hello world\n");
}
https.createServer(httpsOptions, app).listen(4433);
Enter fullscreen mode Exit fullscreen mode

If you also want to support http requests, you need to make just this small modification :

var http = require('http');
var https = require('https');
var fs = require('fs');
var httpsOptions = {
 key: fs.readFileSync('path/to/server-key.pem'),
 cert: fs.readFileSync('path/to/server-crt.pem')
};
var app = function (req, res) {
 res.writeHead(200);
 res.end("hello world\n");
}
http.createServer(app).listen(8888);
https.createServer(httpsOptions, app).listen(4433);
Enter fullscreen mode Exit fullscreen mode

3.1. go to the directory where your server.js is located :

cd /path/to
Enter fullscreen mode Exit fullscreen mode

3.2. run server.js :

node server.js
Enter fullscreen mode Exit fullscreen mode

With all that being said, I highly recommend you keep learning!

Thank you for reading this article. Please feel free to connect with me on LinkedIn and Twitter.

Buy Me A Coffee

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Terabox Video Player