高级前端node【Q347】在 node 中如何开启 https

在 node 中如何开启 https

Issue 欢迎在 Gtihub Issue 中回答此问题: Issue 350

Author 回答者: shfshanyue

express 中开启 https,如下代码所示

import path from "path";
import fs from "fs";
import express from "express";
import http from "http";
import https from "https";
 
const app = express();
 
const cred = {
  key: fs.readFileSync(path.resolve(__dirname, "../key.pem")),
  cert: fs.readFileSync(path.resolve(__dirname, "../cert.pem")),
};
const httpServer = http.createServer(app);
const httpsServer = https.createServer(cred, app);
 
httpServer.listen(8000);
httpsServer.listen(8888);