How to set up SSL locally with Node.js?
Tags: node, javascript, express
Published: 2021-07-28 12:38:01

---
title: How to set up SSL locally with Node.js?
published: true
date: 2021-04-01 00:00:00 UTC
tags: node, javascript, express
canonical_url: https://www.mariokandut.com/how-to-set-up-ssl-locally-in-node-js/
---

If you're running your Node.js backend locally its served using HTTP by default. In some cases it is required that your backend is served via https for integrating a service like Azure B2C or similar. This article is about how to configure express.js to serve a Node.js backend over https in local development. Let's see how this can be achieved in Node.js.

If you want to find out how this is achieved in a React app, check out this article - [How to setup ssl in React](https://www.mariokandut.com/how-to-setup-https-ssl-in-localhost-react/), for Angular check out this article - [How to setup ssl in Angular](https://www.mariokandut.com/how-to-configure-https-ssl-in-angular/).

## [<svg aria-hidden="true" focusable="false" height="16" version="1.1" viewbox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg>](#using-https-in-development)Using HTTPS in development

To use https locally, we have to do the following:

1. Generate local Certificate Authority, and an SSL certificate
2. Set the certificates when serving the Node.js backend
3. Testing!

### [<svg aria-hidden="true" focusable="false" height="16" version="1.1" viewbox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg>](#custom-ssl-certificate)Custom SSL certificate

You have to create a local Certificate Authority, and an SSL certificate and set the `SSL_CERT_FILE` and `SSL_KEY_FILE` to the generated files.

#### [<svg aria-hidden="true" focusable="false" height="16" version="1.1" viewbox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg>](#generating-an-ssl-certificate)Generating an SSL Certificate

As the first step, you should generate a local Certificate Authority, and an SSL certificate for Local Development.

1. 

You need a package manager to install mkcert:

  - MacOS: Use [Homebrew](https://brew.sh/) or [Macports](https://www.macports.org/).
  - Linux: Use **certutil**. Arch Linux only, mkcert is available on the Arch Linux repository.
  - Windows: Use [chocolatey](https://chocolatey.org/).
2. Install [mkcert](https://github.com/FiloSottile/mkcert).
3. Create a locally trusted CA with `mkcert -install`.
4. Generate an SSL certificate with `mkcert localhost`.

### [<svg aria-hidden="true" focusable="false" height="16" version="1.1" viewbox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg>](#set-custom-ssl-certificate)Set custom SSL certificate

To serve an Express.js app locally with SSL we have to update the options object - `key` and `cert` properties. Hence, after generating the local certificate authority and ssl certificate we have to set the `key` and `cert` properties to the path of the certificate and key files.

Let's look at a simple express server. The variables `CERT-PATH` and `KEY-PATH` are the paths to the generated files.

Create or add a project folder.

```
mkdir node-ssl-test
```

Initialize project with `npm init -y` to be able to install node packages.

```
cd node-ssl-test
npm init -y
```

Install `express`.

```
npm install express
```

Create an `index.js` file.

```
touch index.js
```

Copy example code.

```
const https = require('https');
const fs = require('fs');
const express = require('express');

const app = express();

const options = {
  key: fs.readFileSync(CERT_PATH),
  cert: fs.readFileSync(KEY_PATH),
};

app.use((req, res, next) => {
  res.send('<h1>HTTPS is working!</h1>');
});

const port = 3000;

https.createServer(options, app).listen(port, () => {
  console.log('Server listening on port ' + port);
});
```

Now run the `index.js` file with `node index.js` and open a browser tab and navigate to `https://localhost:3000`, you should see **HTTPS works!**. You can also inspect the certificate in the browser dev tools (Chrome -> Security Tab or Lock Icon).

## [<svg aria-hidden="true" focusable="false" height="16" version="1.1" viewbox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg>](#tldr)TL;DR

- To enable HTTPS locally, local certificate authority and ssl certificate have to be created and set in the options object in the Express.js server.

Thanks for reading and if you have any **questions** , use the **comment** function or **send me a message** **[@mariokandut](https://www.twitter.com/mariokandut)**.

If you want to know more about **[Node](https://www.mariokandut.com/tags/node)**, have a look at these [Node Tutorials](https://www.mariokandut.com/tags/node).

**References (and Big thanks):**

[bitsrc.io - dulanka](https://blog.bitsrc.io/using-https-for-local-development-for-react-angular-and-node-fdfaf69693cd),[chocolatey](https://chocolatey.org/),[mkcert](https://github.com/FiloSottile/mkcert)