当前位置: 首页 > 后端技术 > Node.js

快递使用https服务

时间:2023-04-03 21:07:26 Node.js

1.生成证书opensslgenrsa1024>./private.pem#生成私钥密钥文件opensslreq-new-key./private.pem-outcsr.pem#通过私钥文件生成CSR证书签名opensslx509-req-days365-incsr.pem-signkey./private.pem-out./file.crt#通过签署私钥文件和CSR证书生成证书文件]2.注入证书consthttp=require('http');constserver=http.createServer(app);//替换代码constport=normalizePort(process.env.PORT||'443');constpath=require('path');consthttps=require('https');constfs=require('fs');constoptions={key:fs.readFileSync(path.join(__dirname,'./private.pem')),cert:fs.readFileSync(path.join(__dirname,'./file.crt'))}//创建服务varhttpsServer=https.createServer(options,app);varhttpServer=http.createServer(app);////https监听端口3000httpsServer.listen(3001,'localhost',()=>{console.log('Exampleapplisteningonport3000!')});//http监听端口3001httpServer.listen(3000,'localhost',()=>{console.log('Exampleapplisteningonport3000!')});网上有很多例子https://blog.csdn.net/f826760951/a文章/details/67639309https://blog.csdn.net/u012353243/article/details/53409159