Https server from node js

Gaya's techtips
1 min readJun 11, 2021

You can create https server from node js without any other classes.let’s see how to create https server from node js without any library.

First of all we need to have an open ssl for creating key and certificate. If you have an ssl certificate provider you can use it or you can create an ssl server yourself using open ssl. You need to install open ssl in your PC if you are linux user you can install open ssl using following command

sudo apt-get intall openssl

Then you can create an open ssl certificate and key by simple command. Following command may help you to install. Open ssl on your PC.

openssl genrsa -out key.pem
openssl req -new -key key.pem -out csr.pem
openssl x509 -req -days 9999 -in csr.pem -signkey key.pem -out cert.pem
rm csr.pem

Then you need to import fs and https for your node js application to create node https server.Then you need to import key and certificate to constant. Then you can create simple code for run smooth server program for very basic function

const https = require(‘https’)const fs = require(‘fs’)const opt = {key: fs.readFileSync(‘key.pem’),cert:fs.readFileSync(‘cert.pem’)}https.createServer(opt, (req, res) => {res.writeHead(200);res.end(“Hello World!”);}).listen(8000);

you can see some error message in web browser because this is self signed. if you got ssl certificate from valid provider this error may not occur.if this is good you can clap and if this is not enough good or there are problem you can comment it

--

--