DEV Community

Cover image for HTTPS Localhost For Node.JS
Josue Bustos
Josue Bustos

Posted on • Updated on

HTTPS Localhost For Node.JS

Do you need a secure connection for your local Node.js web server? No problem! This tutorial will show you how to create a self-assigned certificate on your local machine using a few CLI commands.

Note: I don't maintain the source code. Specific OS trouble shooting can be found here.

Prerequisites

This guide assumes you're comfortable executing terminal commands, navigating, and modifying the file system.

To follow along, you will need to have the following installed on your host machine:

  • OpenSSL
  • Homebrew
  • Terminal
  • Node.js
  • macOS

Not your OS? Not to worry. Linux and Windows MAY require just a tad few more steps. I promise! You can find those steps here.

Let's get started, shall we...

Create A Node.js Project

Create a folder and change directories. We can do this in one line, like so:

$ mkdir sample-project && cd sample-project
Enter fullscreen mode Exit fullscreen mode

Next, create a file inside the project root directory:

$ touch server.js
Enter fullscreen mode Exit fullscreen mode

Copy-paste the sample code below into the server.js file:

// server.js
const https = require('https');
const fs = require('fs');

const options = {
  key: fs.readFileSync('key.pem'),
  cert: fs.readFileSync('cert.pem')
};

const app = function (req, res) {
  res.writeHead(200);
  res.end("hello world\n");
}

https.createServer(options, app).listen(3000);
Enter fullscreen mode Exit fullscreen mode

If you launch the server now, it will output an error. Why? Because we have not generated our two pem files referenced in the const options variable.

Create Self-Assigned Certificate

To generate a self-assigned certificate on your host machine, you need to install mkcert using Homebrew like so:

$ brew install mkcert
Enter fullscreen mode Exit fullscreen mode

Generate a certificate locally

Navigate to your Node.js project root directory, open your preferred terminal app, type the following command, and then press Return(enter).

It's essential that you add a localhost string at the end to allow HTTPS to secure your localhost URL.

$ mkcert -key-file key.pem -cert-file cert.pem example.com *.example.com localhost

Enter fullscreen mode Exit fullscreen mode

This command will generate two pem files.

  • key.pem
  • cert.pem

If you noticed, the names of these two files reflect the two objects in the const options variable. You project setup should now look similar to the image below.

your code

Verify HTTPS Connection

To verify that everything is working, launch the Node.js server by typing this command in the terminal.

$ node server.js
Enter fullscreen mode Exit fullscreen mode

Next, open a browser, preferably a Chromium OS-based browser, and verify the HTTPS connection in the search bar to the far left has a secure HTTPS icon. See example images below.

hello world

Another way to verify your new HTTPS connection is using the web console provided by the browser. Simply navigate to the Security tab.

web console

I will guarantee you that this quick setup will save you hundreds if not thousands of hours googling. I know it did for me!

Note, that this is not a replacement for a real-world secure HTTPS connection and you're expected to implement secure endpoints anywhere and everywhere where security is required and or needed.

I hope you enjoyed this tutorial. Until next time!

Top comments (2)

Collapse
 
ewaldvanrooyen profile image
Info Comment hidden by post author - thread only accessible via permalink

Very cool article. I seemed to run into an issue with the mkcert command. I solved it by removing the wild card example.com.

The create cert command does not seem to work.
Error: no matches found: *.example.com

Collapse
 
andreweastwood profile image
Andrew Eastwood

neat!

Some comments have been hidden by the post's author - find out more