This repository demonstrates how to create a secure SSL/TLS-backed web service using Node.js. The web service uses self-signed certificates generated via a custom Certificate Authority (CA).
- HTTPS support with custom CA certificates
- Basic web service created using Node.js and Express
- Secure communication with SSL/TLS encryption
Before you begin, ensure you have the following installed:
- Node.js (v14+ recommended)
- OpenSSL (to generate the certificates)
openssl genrsa -out ca.key 2048
openssl req -x509 -new -nodes -key ca.key -sha256 -days 3650 -out ca.crtopenssl genrsa -out server.key 2048
openssl req -new -key server.key -out server.csropenssl x509 -req -in server.csr -CA ca.crt -CAkey ca.key -CAcreateserial -out server.crt -days 365 -sha256This process generates the following files:
ca.key: Private key for the CAca.crt: Certificate for the CAserver.key: Private key for the serverserver.csr: Certificate Signing Request for the serverserver.crt: Server certificate signed by the CA
Run the following command to install necessary packages:
npm install expressCreate a file named server.js and use the following code:
const https = require("https");
const fs = require("fs");
const express = require("express");
const app = express();
// Load the server certificate and private key
const options = {
key: fs.readFileSync("server.key"),
cert: fs.readFileSync("server.crt"),
ca: fs.readFileSync("ca.crt"),
};
// Define a test route
app.get("/", (req, res) => {
res.send("Welcome to the SSL-backed Node.js web service!");
});
// Create an HTTPS server
https.createServer(options, app).listen(443, () => {
console.log("Server is running at https://localhost");
});Start the server with:
node server.jsVisit https://localhost in your browser.
Note: You may encounter a security warning because your custom CA is not trusted by default.
To avoid browser warnings, trust the CA certificate:
Copy ca.crt to /usr/local/share/ca-certificates/ and update certificates:
sudo cp ca.crt /usr/local/share/ca-certificates/
sudo update-ca-certificatesImport ca.crt into Keychain Access and mark it as "Always Trust."
Use the Certificate Manager to import ca.crt into the "Trusted Root Certification Authorities" store.
.
├── ca.crt # Certificate Authority certificate
├── ca.key # Certificate Authority private key
├── server.crt # Server certificate
├── server.csr # Server certificate signing request
├── server.key # Server private key
├── server.js # Node.js web service
├── package.json # Node.js dependencies
└── README.md # Documentation
This setup uses self-signed certificates for development and testing. For production environments, it’s recommended to use certificates from a trusted CA like Let's Encrypt.
To enable mutual TLS (client and server authentication):
- Generate client certificates similarly to the server certificates.
- Update
server.jsto validate client certificates by settingrequestCert: truein the HTTPS options.
This project is licensed under the MIT License. Feel free to modify and use it as needed.