Express配置ssl证书,为网站开启https


本文不对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}`);
});

  


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM