国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 服務器 > Web服務器 > 正文

Node啟動https服務器的教程

2024-09-01 13:54:12
字體:
來源:轉載
供稿:網友

首先你需要生成https證書,可以去付費的網站購買或者找一些免費的網站,可能會是key或者crt或者pem結尾的。不同格式之間可以通過OpenSSL轉換,如:

openssl x509 -in mycert.crt -out mycert.pem -outform PEM

Node原生版本:

const https = require('https')const path = require('path')const fs = require('fs')// 根據項目的路徑導入生成的證書文件const privateKey = fs.readFileSync(path.join(__dirname, './certificate/private.key'), 'utf8')const certificate = fs.readFileSync(path.join(__dirname, './certificate/certificate.crt'), 'utf8')const credentials = { key: privateKey, cert: certificate,}// 創建https服務器實例const httpsServer = https.createServer(credentials, async (req, res) => { res.writeHead(200) res.end('Hello World!')})// 設置https的訪問端口號const SSLPORT = 443// 啟動服務器,監聽對應的端口httpsServer.listen(SSLPORT, () => { console.log(`HTTPS Server is running on: https://localhost:${SSLPORT}`)})

express版本

const express = require('express')const path = require('path')const fs = require('fs')const https = require('https')// 根據項目的路徑導入生成的證書文件const privateKey = fs.readFileSync(path.join(__dirname, './certificate/private.key'), 'utf8')const certificate = fs.readFileSync(path.join(__dirname, './certificate/certificate.crt'), 'utf8')const credentials = { key: privateKey, cert: certificate,}// 創建express實例const app = express()// 處理請求app.get('/', async (req, res) => { res.status(200).send('Hello World!')})// 創建https服務器實例const httpsServer = https.createServer(credentials, app)// 設置https的訪問端口號const SSLPORT = 443// 啟動服務器,監聽對應的端口httpsServer.listen(SSLPORT, () => { console.log(`HTTPS Server is running on: https://localhost:${SSLPORT}`)})

koa版本

const koa = require('koa')const path = require('path')const fs = require('fs')const https = require('https')// 根據項目的路徑導入生成的證書文件const privateKey = fs.readFileSync(path.join(__dirname, './certificate/private.key'), 'utf8')const certificate = fs.readFileSync(path.join(__dirname, './certificate/certificate.crt'), 'utf8')const credentials = { key: privateKey, cert: certificate,}// 創建koa實例const app = koa()// 處理請求app.use(async ctx => { ctx.body = 'Hello World!'})// 創建https服務器實例const httpsServer = https.createServer(credentials, app.callback())// 設置https的訪問端口號const SSLPORT = 443// 啟動服務器,監聽對應的端口httpsServer.listen(SSLPORT, () => { console.log(`HTTPS Server is running on: https://localhost:${SSLPORT}`)})

總結

以上所述是小編給大家介紹的Node啟動https服務器的教程,希望對大家有所幫助,如果大家有任何疑問歡迎給我留言,小編會及時回復大家的!


注:相關教程知識閱讀請移步到服務器教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 密山市| 彝良县| 鄂温| 庐江县| 宣城市| 舞阳县| 伊吾县| 泗阳县| 河曲县| 平昌县| 扶绥县| 玛曲县| 城市| 邯郸市| 辽阳县| 贵港市| 昌邑市| 瑞丽市| 河津市| 张北县| 临潭县| 丹巴县| 海淀区| 平顶山市| 三明市| 泸定县| 光泽县| 剑阁县| 乌海市| 稷山县| 永和县| 嵩明县| 泽州县| 苍山县| 赣州市| 光泽县| 安顺市| 阳泉市| 舞阳县| 山东省| 淳化县|