1

1.生成证书


openssl genrsa 1024 > ./private.pem #生成私钥key文件

openssl req -new -key ./private.pem -out csr.pem #通过私钥文件生成CSR证书签名

openssl x509 -req -days 365 -in csr.pem -signkey ./private.pem -out ./file.crt #通过私钥文件和CSR证书签名生成证书文件
]

2. 注入证书

const http = require('http');
const server = http.createServer(app);

//替换代码
const port = normalizePort(process.env.PORT || '443');
const path = require('path');
const https = require('https');
const fs = require('fs');
const options = {
  key: fs.readFileSync(path.join(__dirname, './private.pem')),
  cert: fs.readFileSync(path.join(__dirname, './file.crt'))
}

// 创建服务
var httpsServer = https.createServer(options, app);
var httpServer = http.createServer(app);
// //https监听3000端口
httpsServer.listen(3001, 'localhost', () => {
    console.log('Example app listening on port 3000!')
});
//http监听3001端口
httpServer.listen(3000, 'localhost', () => {
    console.log('Example app listening on port 3000!')
});

网上还有很多例子

https://blog.csdn.net/f826760951/article/details/67639309

https://blog.csdn.net/u012353243/article/details/53409159


木子喵
492 声望26 粉丝

too young, too naive