---
title: "How to Create an HTTPS NodeJS Web Sevice with Express"
description: "Learn how to set up an HTTPs NodeJS server using the Express.js web framework in this step-by-step guide!"
canonical: "https://adamtheautomator.com/https-nodejs/"
---

# How to Create an HTTPS NodeJS Web Sevice with Express

> Learn how to set up an HTTPs NodeJS server using the Express.js web framework in this step-by-step guide!

Source: https://adamtheautomator.com/https-nodejs/

---

ATA Learning

Tap to hide

[

ATA Learning

](/)

*   [Home](/)
*   [Tutorials](/tutorials/)
*   [Instructors](/author/)
*   [Advertising](/advertising/)
*   [Recommended Resources](/resources/)
*   [About Adam](/about-adam/)

Search for:  

*   [](https://twitter.com/adbertram)
*   [](https://github.com/Adam-the-Automator)
*   [](https://www.linkedin.com/company/adam-the-automator-llc)
*   [](/feed/)

![How to Create an HTTPS NodeJS Web Service with Express](https://adamtheautomator.com/wp-content/uploads/2021/11/How-to-Create-an-HTTPS-NodeJS-Web-Sevice-with-Express.jpg)

# How to Create an HTTPS NodeJS Web Service with Express

[![](https://secure.gravatar.com/avatar/1117c2efdf8de21446d7ab331de2a85cb0b9b13ce18b09d9f7bc369451f2ce77?s=192&d=mm&r=g)Ekekenta Odionyenfe .C](https://adamtheautomator.com/author/ekekenta-odionyenfe/)16 November 20214 min. read

Categories: [IT Ops](/category/it-ops/)

Tags:[NodeJS](/tag/nodejs/)

Table of Contents

*   [Prerequisites](#prerequisites)
*   [Creating a NodeJS Project](#creating-a-nodejs-project)
*   [Installing Express](#installing-express)
*   [Creating a Web Service](#creating-a-web-service)
*   [Creating an SSL Certificate](#creating-an-ssl-certificate)
*   [Enabling HTTPS Nodejs in Express](#enabling-https-in-express)
*   [Conclusion](#conclusion)

When hosting NodeJS web applications with external APIs, it’s essential to keep communication secure. By setting up [a popular NodeJS web framework called Express](https://expressjs.com/) and configuring API endpoints to communicate via HTTPS NodeJS is a great way to do that.

In this tutorial, you will learn how to install and configure the Express NodeJS application framework and set up an encrypted API endpoint to keep communication encrypted.

Let’s get started!

## Prerequisites

This tutorial is a hands-on demonstration. To follow along, be sure you have the following in place:

*   A Linux machine – This tutorial will use Ubuntu 20.04.
*   [NodeJS](https://nodejs.org/en/) – NodeJS acts as the runtime environment for all of your Express projects.

## Creating a NodeJS Project

Once you have NodeJS installed on your server, it’s time to set up Express. The easiest way to install Express is via a NodeJS package. But before you do that, first create a directory for this demo project. This directory will be located in _~/NodejsHTTPSServer_ for this tutorial.

```powershell
mkdir ~/NodejsHTTPSServer
```

Next, initialize a new NodeJS project using `npm init`. This command will create a [package.json](https://docs.npmjs.com/cli/v7/configuring-npm/package-json/) file in the directory you just created, recording necessary metadata about a project you are building.

Use the `-y` flag, as shown below to accept the default values. You do not need any unique NodeJS project settings for this tutorial. This action will create a NodeJS project with the same name as the folder (`NodejsHTTPSServer`).

```bash
npm init -y
```

## Installing Express

Now it’s time to install the Express package. To do so, run the command below to install the NodeJS Express package. This command will install the NodeJS Express package save the information in the _package.json_ file, as shown below.

```bash
npm install express
```

![nstalling Express.js](https://adamtheautomator.com/wp-content/uploads/2021/11/image-200.png)

nstalling Express.js

## Creating a Web Service

It’s now time to create a web service/server with Express!

1\. To get started, ensure you’re still in the _~/NodejsHTTPSServer_ directory and create a blank file called _index.js_. This file will be a Javascript script that will hold all necessary code that NodeJS will execute when launching the web service.

```bash
touch index.js
```

2\. Next, open the _index.js_ file and add the following code to it. This file leverages both built-in NodeJS modules and the installed `express` module to bring up a web service listening on port 4000 when executed.

```javascript
// Import builtin NodeJS modules to instantiate the service
const https = require("https");
const fs = request("fs");

// Import the express module
const express = require("express");

// Instantiate an Express application
const app = express();

// Create a NodeJS HTTPS listener on port 4000 that points to the Express app
// Use a callback function to tell when the server is created.
https
  .createServer(app)
  .listen(4000, ()=>{
    console.log('server is runing at port 4000')
  });

// Create an try point route for the Express app listening on port 4000.
// This code tells the service to listed to any request coming to the / route.
// Once the request is received, it will display a message "Hello from express server."
app.get('/', (req,res)=>{
    res.send("Hello from express server.")
})
```

3\. Once you’ve created the Express project, tell NodeJS to run it to bring up the web service.

```bash
node index.js
```

If successful, you will see the callback function defined above invoked to return a message letting you know it has started.

![Running express Server](https://adamtheautomator.com/wp-content/uploads/2021/11/image-201.png)

Running express Server

4\. Finally, test the service by bringing up a web browser and navigating to _http://localhost:4000/ i_n your browser. If successful, you should see a message “Hello from express server.”.

## Creating an SSL Certificate

At this point, your NodeJS app doesn’t yet support HTTPS. It’s working with unencrypted HTTP, but that’s not secure. Let’s change that.

Related:[Your Guide to X509 Certificates (For Mortals)](https://adamtheautomator.com/x509-certificates/)

To configure an SSL certificate for our NodeJS HTTPS implementation, you can either use a [public, trusted certificate](https://www.ssl.com/faqs/what-is-a-certificate-authority/) or a self-signed certificate. This tutorial will use a self-signed certificate signed locally by the host as it’s much easier for proof of concept projects.

> _If you’re running a NodeJS HTTPS application with Express in a production environment, always be sure to acquire and install a trusted certificate!_

Related:[New-SelfSignedCertificate: Creating Certificates with PowerShell](https://adamtheautomator.com/new-selfsignedcertificate/)

1\. First, generate a key file used for self-signed certificate generation with the command below. The command will create a private key as a file called _key.pem._

```bash
openssl genrsa -out key.pem
```

2\. Next, generate a certificate service request (CSR) with the command below. You’ll need a CSR to provide all of the input necessary to create the actual certificate.

```bash
openssl req -new -key key.pem -out csr.pem
```

![Generating SSL Key](https://adamtheautomator.com/wp-content/uploads/2021/11/image-202.png)

Generating SSL Key

3\. Finally, generate your certificate by providing the private key created to sign it with the public key created in step two with an expiry date of 9,999 days. This command below will create a certificate called _cert.pem_.

```bash
openssl x509 -req -days 9999 -in csr.pem -signkey key.pem -out cert.pem
```

Once complete, your project folder should look like below:

```powershell
NodejsHTTPSServer
	/cert.pem
	/csr.pem
	/index.js
	/key.pem
	/package-lock.json
	/package.json
```

## Enabling HTTPS Nodejs in Express

At this point, your SSL certificate has been successfully created. Now it’s time to enable HTTPS with NodeJS and Express. To enable HTTPS requires slightly modifying the Express app you created earlier. Check [this](https://adamtheautomator.com/search/https) if you like to redirect your HTTP to HTTPS Traffic in NGINX.

Related:[How to Redirect HTTP to HTTPS Traffic in NGINX](https://adamtheautomator.com/search/https)

Open the _index.js_ file you created earlier and modify the `createServer()` method, as shown below. You’ll notice that you must provide the private key (`key.pem`) and the public key/certificate (`cert.pem`) to the `createServer()` method.

The below example reads both private and public keys, which only execute once when the service starts.

```javascript
https
  .createServer(
		// Provide the private and public key to the server by reading each
		// file's content with the readFileSync() method.
    {
      key: fs.readFileSync("key.pem"),
      cert: fs.readFileSync("cert.pem"),
    },
    app
  )
  .listen(4000, () => {
    console.log("serever is runing at port 4000");
  });
```

With that done, your express HTTPS server has been successfully created. Now test the application in your browser by going to [](https://localhost:4000/)https://localhost:4000.

You should still get the same response of **“Hello from express server”** on your browser.

![Testing application in a browser](https://adamtheautomator.com/wp-content/uploads/2021/11/image-203.png)

Testing application in a browser

> _Note that if you can’t initially browse to the web service with HTTPS, you may need to [terminate and restart the server](https://gajus.medium.com/how-to-terminate-a-http-server-in-node-js-d374f8b8c17f)._

## Conclusion

You should now understand how to set up a NodeJS HTTPS web service using the Express framework. By simply installing the Express NodeJS package and creating a simple configuration script, you can have a secure web service running over HTTPS.

How do you foresee using this web service script to host NodeJS HTTPS web services with Express?

Share this article

[Share on X](https://twitter.com/intent/tweet?url=https%3A%2F%2Fadamtheautomator.com%2Fhttps-nodejs%2F&text=How%20to%20Create%20an%20HTTPS%20NodeJS%20Web%20Service%20with%20Express)[Share on Facebook](https://www.facebook.com/sharer/sharer.php?u=https%3A%2F%2Fadamtheautomator.com%2Fhttps-nodejs%2F)[Share on LinkedIn](https://www.linkedin.com/sharing/share-offsite/?url=https%3A%2F%2Fadamtheautomator.com%2Fhttps-nodejs%2F)

## Related Posts

![](https://adamtheautomator.com/wp-content/uploads/2022/04/Utilize-Docker-with-this-MERN-Stack-Tutorial-Solution.jpg)

### [Utilize Docker with this MERN Stack Tutorial Solution](/mern-stack-tutorial/)

Learn how to leverage Docker and make your application compatible with different services in this MERN stack tutorial!

![](https://adamtheautomator.com/wp-content/uploads/2026/07/featured_image-4.png)

### [Migrate from EWS to Microsoft Graph API](/migrate-ews-microsoft-graph-api/)

Move EWS mailbox automation to Microsoft Graph with a practical inventory, permissions, PowerShell SDK, and cutover validation checklist.

![](https://adamtheautomator.com/wp-content/uploads/2026/06/featured_image-21.png)

### [Automate SOC 2 Compliance with PowerShell](/automate-soc-2-compliance-powershell/)

A comprehensive walkthrough on using PowerShell and Azure Policy as Code to automate evidence collection and enforce SOC 2 compliance controls across enterprise cloud environments.

## Categories

*   [IT Ops](/category/it-ops/)
*   [Cloud](/category/cloud/)
*   [DevOps](/category/devops/)
*   [Home Ops](/category/home-ops/)
*   [Information Security](/category/infosec/)
*   [Software Development](/category/software-development/)

## Site

*   [Home](/)
*   [Tutorials](/tutorials/)
*   [Instructors](/author/)
*   [Advertising](/advertising/)
*   [Recommended Resources](/resources/)
*   [About Adam](/about-adam/)

Copyright 2026© ATA Learning | [Privacy Policy](/privacy/)
