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