node:https
创建一个基于 HTTPS 协议的服务器
创建一个本地的 https 服务
使用 mkcert 创建 cert 证书和 private key 文件:
$ mkcert localhost然后创建一个 https 服务器:
import https from 'https';
import fs from 'fs';
const options = {
key: fs.readFileSync('./localhost-key.pem'),
cert: fs.readFileSync('./localhost.pem'),
}
https.createServer(options, (req, res) => {
res.writeHead(200);
res.end('Hello, HTTPS World!');
}).listen(443, () => {
console.log('Server is running on port 443');
});