我在 Node.js 中有这个:
const http2 = require('http2')
const fs = require('fs')
let server
start({
port: process.env.PORT || 3000,
private: fs.readFileSync('/Users/me/certs/localhost.key').toString(),
public: fs.readFileSync('/Users/me/certs/localhost.crt').toString()
})
async function start(opts) {
server = await createServer(opts)
}
async function createServer({ port, private, public, password }) {
return new Promise((res, rej) => {
let server = http2.createSecureServer({
key: private,
cert: public,
passphrase: password,
allowHTTP1: true,
secureProtocol: 'TLSv1_2_method'
}, handleServerRequest)
server.listen(port, fault => {
if (fault) {
rej(fault)
} else {
res()
}
})
})
}
async function handleServerRequest(req, res) {
console.log('here')
}
我生成了我的证书(并且我认为将它添加到我的电脑上的信任存储中)像这样:
mkdir ~/certs
cd ~/certs
openssl req -x509 -sha256 -nodes \
-subj '/CN=localhost' \
-newkey rsa:2048 -days 365 \
-keyout localhost.key -out localhost.crt
open localhost.crt # add it to something? login?
sudo security add-trusted-cert \
-p ssl -d -r trustRoot \
-k ~/Library/Keychains/login.keychain localhost.crt
但是,运行 Node.js 服务器并访问后https://localhost:3000
,我得到了以下信息:
我做错了什么?我该如何解决?