本文不对express多做介绍,下面直奔主题:
一、下载证书(以腾讯云为例):
解压下载的压缩包,找到Nginx文件夹,里面有两个以crt和key结尾的文件,在你的项目根目录新建名为https的空文件夹,把crt文件和key文件复制到https文件夹中;
二、项目配置:
1、找到bin文件夹下的www文件(你的项目中启动node server的文件,如果没有,那应该是app.js);
2、在文件头部加入以下代码:
var app = require('../app'); var https = require('https'); var fs = require('fs'); var path = require('path'); var privateCrt = fs.readFileSync(path.join(process.cwd(), 'https/1_www.kakayang.cn_bundle.crt'), 'utf8'); var privateKey = fs.readFileSync(path.join(process.cwd(), 'https/2_www.kakayang.cn.key'), 'utf8'); const HTTPS_OPTOIN = { key: privateKey, cert: privateCrt }; const SSL_PORT = 443; const httpsServer = https.createServer(HTTPS_OPTOIN, app); httpsServer.listen(SSL_PORT, () => { console.log(`HTTPS Server is running on: https://localhost:${SSL_PORT}`); });