Creating a Local HTTPS NestJS Server with Free SSL Certificates: A Step-by-Step Guide

Developing applications locally with secure connections is essential for testing and debugging, especially when dealing with HTTPS. In this guide, we'll walk through the process of setting up a NestJS server locally localhost with HTTPS, using free SSL certificates generated by mkcert.

We'll also address common issues, such as SSL verification problems in Postman.

⚡ Prerequisites

Before we begin, ensure you have the following tools installed:

  1. Node.js and npm

  2. NestJS CLI

  3. mkcert

⚡ Step 1: Create a NestJS Project

If you don't have a NestJS project, create one using the following commands:

$ npm i -g @nestjs/cli
$ nest new project-name
$ cd project-name

⚡ Step 2: Install Required Packages

Install mkcert from their Cert Official Github, according to your operating system. We will use mkcert it for generating the SSL certificate locally in your project.

⚡ Step 3: Generate SSL Certificates with mkcert

After the installation, ensure that the src/cert directory exists in your project.

Let's generate an SSL certificate now:

mkdir -p src/cert # if you don't have the src/cert directory
mkcert -install
mkcert -key-file ./src/cert/key.pem -cert-file ./src/cert/cert.pem localhost

⚡ Step 4: Update NestJS Configuration

Edit the main.ts file to use HTTPS:

import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import * as fs from 'fs';

async function bootstrap() {
  const httpsOptions = {
    key: fs.readFileSync('./src/cert/key.pem'),
    cert: fs.readFileSync('./src/cert/cert.pem'),
  };

  const app = await NestFactory.create(
    AppModule,
    { httpsOptions },
  );

  await app.listen(3000);
}

bootstrap();

⚡ Step 5: Run the Server

Start your NestJS server:

npm run start:dev

Visit https://localhost:3000 in your browser to verify that your server is running with HTTPS.

⚡ Step 6: Handling SSL Verification Issues in Postman

If you encounter an SSL error in Postman, "Unable to verify the first certificate," you have a few options:

  • Option 1: Trust the Self-Signed Certificate in Postman

    • Open Postman, go to File > Settings > General, and toggle off "SSL certificate verification."
  • Option 2: Use a Valid SSL Certificate

    • For production, obtain a valid SSL certificate from a trusted authority.
  • Option 3: Add the Certificate to Trusted Authorities in Postman

    • Open the certificate file (./src/cert/cert.pem), copy the content, go to Postman Settings > Certificates, and paste the certificate under the "CA Certificates" tab.

Remember, using self-signed certificates is suitable for development, but for production, always use valid certificates from trusted authorities.

Now you have a local NestJS server running with HTTPS, equipped with free SSL certificates.

Happy coding! 🚀


I hope this article was a good read for you. Do share it with your friends and other peers.

Thank you so much! ❤️

👉 Follow Me: GitHub Twitter LinkedIn Youtube