DEV Community

FredAbod
FredAbod

Posted on

Building an Express App with an HTTPS Server

Building an Express App with an HTTPS Server

In this tutorial, we will walk through the process of building a basic Express application with an HTTPS server. The code provided will enable you to run your application over a secure HTTPS connection, allowing for encrypted communication between the client and the server.

Prerequisites

To follow along with this tutorial, you should have a basic understanding of Node.js and Express.js. Make sure you have Node.js and npm (Node Package Manager) installed on your machine.

Setting up the Project

  1. Start by creating a new directory for your project and navigate into it using the command line.

  2. Initialize a new Node.js project by running the following command:

npm init -y

This will create a new package.json file for your project.

  1. Install the required dependencies by running the following command:

npm i express dotenv morgan

This will install the necessary packages: express, dotenv, and morgan.

Writing the Code

Create a new file named index.js and open it in a text editor. Copy the following code into the file:



const express = require("express");
const dotenv = require("dotenv").config();
const https = require("https");
const fs = require("fs");
const path = require("path");

const app = express();
const morgan = require("morgan");
const port = process.env.PORT || 4500;

// Middleware
app.use(morgan("dev"));

// Routes
app.get("/", (req, res) => {
res.send("WELCOME TO THE BASIC EXPRESS APP WITH AN HTTPS SERVER");
});

// Read SSL certificate and key files
const options = {
key: fs.readFileSync(path.join(dirname, "localhost-key.pem")),
cert: fs.readFileSync(path.join(dirname, "localhost.pem")),
};

// Create HTTPS server
const server = https.createServer(options, app);

server.listen(port, () => {
console.log(App listening on https://localhost:</span><span class="p">${</span><span class="nx">port</span><span class="p">}</span><span class="s2">);
});

Enter fullscreen mode Exit fullscreen mode




Now let's go through the code and understand each section:

  • We import the necessary modules: express, dotenv, https, fs, path, and morgan.
  • An instance of the Express app is created using express().
  • We set up the middleware using morgan for logging our requests.
  • A single route is defined for the root URL ("/") to send a welcome message.
  • The SSL certificate and key files (localhost-key.pem and localhost.pem) are read using the fs.readFileSync() method. Make sure these files are present in the same directory as the index.js file or adjust the file paths accordingly.
  • An options object is created with the key and certificate, which will be used to create the HTTPS server.
  • Finally, the HTTPS server is created using https.createServer() and listens on the specified port.

Now To The Very Important Part

Generating our localhost-key.pem and localhost.pem. We use this commands on our terminals in the same directory as the index.js file

openssl genrsa -out localhost-key.pem 2048

openssl req -new -x509 -sha256 -key localhost-key.pem -out localhost.pem -days 365

And that's it, start your server and listen on the port you've choosen.

image
Do not forget to create a .env file and add your PORT

Top comments (1)

Collapse
 
sunil_kumar_8b60f0a14afac profile image
Sunil Kumar

this is very helpful