Node.js › HTTP & HTTPS Modules
How does Node's node:https module differ from node:http, and what additional configuration does it require?
IntermediateAnswer
The https module provides the same API surface as http but wraps connections in TLS encryption -- creating an HTTPS server requires providing a TLS certificate and private key (either self-signed for development, or issued by a trusted certificate authority like Let's Encrypt for production), whereas plain http requires no such configuration at all since it transmits data unencrypted.
const https = require('node:https');
const fs = require('node:fs');
const options = {
key: fs.readFileSync('private-key.pem'),
cert: fs.readFileSync('certificate.pem'),
};
https.createServer(options, (req, res) => {
res.end('Secure response');
}).listen(443);Real-world example A production Node.js application typically doesn't run its own https.createServer() at all -- instead, TLS termination is handled by a reverse proxy like Nginx or a cloud load balancer in front of it, with the Node.js process itself only ever handling plain HTTP behind that already-secured boundary.