生成证书
先新建一个配置文件req.cnf
,内容如下
[req]
distinguished_name = req_distinguished_name
x509_extensions = v3_req
prompt = no
[req_distinguished_name]
CN = localhost
[v3_req]
keyUsage = critical, digitalSignature, keyAgreement
extendedKeyUsage = serverAuth
subjectAltName = @alt_names
[alt_names]
DNS.1 = localhost
DNS.2 = 0.0.0.0
注意localhost
也可以改成你的ip或者域名
命令行输入
openssl req -x509 -nodes -days 730 -newkey rsa:2048 \
-keyout cert.key -out cert.pem -config req.cnf -sha256
这里会生成私钥(cert.key)和公钥(cert.pem)
起https服务
新建index.js
const https = require('https');
const fs = require('fs');
const options = {
key: fs.readFileSync('./cert.key'),
cert: fs.readFileSync('./cert.pem'),
};
var server = https.createServer(options, (req, res) => {
res.end('hello');
});
server.listen(8000);
起https服务
命令行输入node index.js
这里我们会起一个端口是8000的HTTPS服务
访问https://localhost:8000/

你也可能遇到如下问题:点击继续前往即可

去除上面的不安全
打开开发者工具里的security

查看电脑安装的证书,点击+,把我们生成的证书添加进去

双击证书,选择始终相信

注意:重启浏览器,完成!!!
可能遇到的问题
chrome://flags/#allow-insecure-localhost
