https
The https
module works like http
but uses TLS/SSL for secure communication. Requires certificates and keys for servers.
API Surface
Server
https.createServer(options, requestListener)
— create HTTPS server.
Options usually include:key
: private keycert
: certificate
- server.listen(port[, host], callback)
- Request/response APIs are identical to http.
Client
https.request(options, callback)
— make HTTPS request.https.get(options, callback)
— shortcut for GET.- Supports additional TLS options like
ca
,rejectUnauthorized
.
Examples (English only)
const https = require("https");
const fs = require("fs");
// HTTPS server with self-signed cert
const options = {
key: fs.readFileSync("key.pem"),
cert: fs.readFileSync("cert.pem")
};
const server = https.createServer(options, (req, res) => {
res.writeHead(200);
res.end("Secure Hello World\n");
});
server.listen(3443, () => console.log("HTTPS server running on port 3443"));
// HTTPS client GET
https.get("https://example.com", (res) => {
console.log("status:", res.statusCode);
res.on("data", (chunk) => console.log("body:", chunk.toString()));
});
Notes
- Requires valid certificate/key for production. Use self-signed only for testing.
- Request and response handling are the same as http.
- Additional TLS options control verification (CA, rejectUnauthorized).
- Use port 443 by convention for HTTPS.