HTTPS Localhost For Node.JS
Tags: node, javascript, security, tooling
Published: 2021-06-06 02:14:28

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](https://github.com/FiloSottile/mkcert/blob/master/README.md#supported-root-stores).

## 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](https://github.com/FiloSottile/mkcert/blob/master/README.md#supported-root-stores).

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:
```bash
$ mkdir sample-project && cd sample-project
```
Next, create a file inside the project root directory:
```bash
$ touch server.js
```

Copy-paste the sample code below into the **server.js** file:
```JavaScript
// 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);
```
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:

```bash
$ brew install mkcert
```
### 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.
```bash
$ mkcert -key-file key.pem -cert-file cert.pem example.com *.example.com localhost

```
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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wcz004zdbi5yedkdf27f.png)

### Verify HTTPS Connection

To verify that everything is working, launch the Node.js server by typing this command in the terminal.
```
$ node server.js
```
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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ocdcepiutvqmri8gzptb.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/iyogjrrd4dlqfcekvwrd.png)

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!