How to Set Up a Free SSL Certificate with Node.js
A complete step-by-step guide to configuring HTTPS on your Node.js server
This guide walks you through setting up a free Let's Encrypt SSL certificate with Node.js using the built-in https module and Express.js.
Prerequisites
- Node.js v14 or later installed on your server
- SSH access to your server
- A domain name pointing to your server
- SSL certificate files from freesslcert.net
Step 1: Generate Your SSL Certificate
Visit freesslcert.net to generate a free certificate for your domain.
Step 2: Upload Certificate Files
Transfer certificate.crt, private.key, and ca_bundle.crt to your project's certs/ directory.
Step 3: Configure the HTTPS Server
Use Node.js https.createServer() with fs.readFileSync() to load your certificate files and create an HTTPS server.
Step 4: Set Up HTTP to HTTPS Redirect
Create a separate HTTP server on port 80 that redirects all requests to HTTPS on port 443.
Step 5: Express.js Configuration
If using Express, pass your Express app to https.createServer() for automatic HTTPS support.