Create ssl / https server in nodejs

Mon Apr 24 2017

Paul Shan

Sometimes there are needs to create https in your node application itself to create real life scenario or may be to test things like http/2 server push. Today I will show you how to create https server in node js, both with and without express.
I will also talk about ssl and what is the significance of it.

A small talk on https

With http or Hyper Text Transfer Protocol two computers (the client and the server) communicates without any encryption. However https means Hyper Text Transfer Protocol Secure and with this protocol the client and the server passes messages with encryption. This is to prevent any hacker to steal the user’s data in between.

A bit on ssl

If you know about https, I’m sure you’ve heard the term SSL too. SSL or Secure Sockets Layer is a standard security technology for establishing an encrypted link between a server and a client. So basically ssl has the key to encrypt or decrypt the informations.
Apart from a key, ssl also has a certificate. This certificate is used to verify your identity. A certificate can be self signed or signed by a CA (Certificate Authority).
Both self or CA signed certificates plays no role on encryption. If you have a self signed certificate, then also the https encryption will work great. But CA signed certificates are trusted. Those authorities are known to browser vendors and also other internet players. Thus, browsers and users will trust your site more if it’s signed by CA.

Create key and certificate

To get a signed by certification authority, you can use websites like godaddy, hostgator etc. And to generate a self signed certificate you can use the following way.

OpenSSL is a command line tool that can be used for generating ssl keys and certificates. In Mac and Linux it will be pre installed, but in windows probably you have to manually install it.

Anyway, assuming you have openssl installed in your machine, below is a command to generate key and certificate. After running the command, it will ask you couple of questions; just answer them.

openssl req -x509 -sha256 -nodes -days 365 -newkey rsa:2048 -keyout privateKey.key -out certificate.crt

Now that we have the private key and certificate with you; it’s time to use them in our node application. Remember you can generate the private key and certificate in any location of your computer. I am putting these two in the root folder.

Create an https node server

var https = require('https');
var fs = require('fs');

var options = {
  key: fs.readFileSync('privateKey.key'),
  cert: fs.readFileSync('certificate.crt')
};

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

console.log("listening to port 8000");

All you need to do is to use the module https instead of http to create the server; and pass the private key and certificate as options. And that’s it. Now you can access https://localhost:8000/. Remember as you are using self signed certificate and not from any CA, the browser will try to reject the connection. But you can anyway proceed to the webpage.

Do it with expressJS

var https = require('https');
var fs = require('fs');
express = require('express'),
app = express();

var options = {
  key: fs.readFileSync('privateKey.key'),
  cert: fs.readFileSync('certificate.crt')
};

https.createServer(options, app).listen(8000);

app.get('/', function (req, res) {
  res.header('Content-type', 'text/html');
  return res.end('Hello World!');
});

console.log("listening to port 8000");

SHARE THIS ARTICLE

post-thumbnail
Today everyone knows the importance of a lightning-fast website and how the speed impacts the conversion rate of a business. Today, everyone wants the site to be a PWA so that the mobile users can have an app-like experience with the website because, for the majority of the merchants, the customers come through mobile devices.
Tue Apr 20 2021
post-thumbnail
Here we are going to see how you can manage backup and restore of Postgres database with docker.
Thu Sep 03 2020
post-thumbnail
Image sliders or carousels always have increased the UI attraction of websites and they are pretty useful for reflecting the major roles/products too. In case, I am having a website that sells tee-shirts,
Mon Apr 30 2018

About VoidCanvas

This blog was created out of hobby and talks mostly about technology, web development, JavaScript, NodeJS and related topics. Thank you for reading my blog.

Copyright 2022 - www.voidcanvas.com

Popular Articles

Authentication using Google's oAuth api with node.js

Thu Mar 10 2016

OAuth authentications are pretty popular now a days and another thing which is popular is JavaScript. This article shows how to plugin google’s oAuth api for authentication in your own node application.

CSS3 Loader Snippet Collection: (Part 2 - Squares)

Sat Mar 01 2014

This is a continuation of my CSS3 loader snippet collection series. I've provided spinning css3 animation loader in the part 1 of this series and here in part 2, I'm providing various square type loading