Node啟動(dòng)https服務(wù)器的教程
首先你需要生成https證書(shū),可以去付費(fèi)的網(wǎng)站購(gòu)買(mǎi)或者找一些免費(fèi)的網(wǎng)站,可能會(huì)是key或者crt或者pem結(jié)尾的。不同格式之間可以通過(guò)OpenSSL轉(zhuǎn)換,如:
openssl x509 -in mycert.crt -out mycert.pem -outform PEM
Node原生版本:
const https = require('https')
const path = require('path')
const fs = require('fs')
// 根據(jù)項(xiàng)目的路徑導(dǎo)入生成的證書(shū)文件
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,
}
// 創(chuàng)建https服務(wù)器實(shí)例
const httpsServer = https.createServer(credentials, async (req, res) => {
res.writeHead(200)
res.end('Hello World!')
})
// 設(shè)置https的訪問(wèn)端口號(hào)
const SSLPORT = 443
// 啟動(dòng)服務(wù)器,監(jiān)聽(tīng)對(duì)應(yīng)的端口
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')
// 根據(jù)項(xiàng)目的路徑導(dǎo)入生成的證書(shū)文件
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,
}
// 創(chuàng)建express實(shí)例
const app = express()
// 處理請(qǐng)求
app.get('/', async (req, res) => {
res.status(200).send('Hello World!')
})
// 創(chuàng)建https服務(wù)器實(shí)例
const httpsServer = https.createServer(credentials, app)
// 設(shè)置https的訪問(wèn)端口號(hào)
const SSLPORT = 443
// 啟動(dòng)服務(wù)器,監(jiān)聽(tīng)對(duì)應(yīng)的端口
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')
// 根據(jù)項(xiàng)目的路徑導(dǎo)入生成的證書(shū)文件
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,
}
// 創(chuàng)建koa實(shí)例
const app = koa()
// 處理請(qǐng)求
app.use(async ctx => {
ctx.body = 'Hello World!'
})
// 創(chuàng)建https服務(wù)器實(shí)例
const httpsServer = https.createServer(credentials, app.callback())
// 設(shè)置https的訪問(wèn)端口號(hào)
const SSLPORT = 443
// 啟動(dòng)服務(wù)器,監(jiān)聽(tīng)對(duì)應(yīng)的端口
httpsServer.listen(SSLPORT, () => {
console.log(`HTTPS Server is running on: https://localhost:${SSLPORT}`)
})
總結(jié)
以上所述是小編給大家介紹的Node啟動(dòng)https服務(wù)器的教程,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)歡迎給我留言,小編會(huì)及時(shí)回復(fù)大家的!
相關(guān)文章
webpack啟動(dòng)服務(wù)器和處理sourcemap的操作方法
Source Map源代碼地圖就是解決此類(lèi)問(wèn)題最好的辦法,從它的名字就能夠看出它的作用:映射轉(zhuǎn)換后的代碼與源代碼之間的關(guān)系,這篇文章主要介紹了webpack啟動(dòng)服務(wù)器和處理sourcemap的操作方法,需要的朋友可以參考下2024-03-03
Mac OSX下使用MAMP安裝配置PHP開(kāi)發(fā)環(huán)境
本部分描述如何在 Mac 上安裝 MAMP。將通過(guò)一個(gè)操作安裝 Apache Web 服務(wù)器、MySQL 和phpMyAdmin,需要的朋友可以參考下2017-09-09
集群運(yùn)維自動(dòng)化工具ansible之使用playbook安裝zabbix客戶(hù)端
Zabbix客戶(hù)端的安裝配置:Zabbix是一個(gè)基于WEB界面的提供分布式系統(tǒng)監(jiān)視以及網(wǎng)絡(luò)監(jiān)視功能的企業(yè)級(jí)的開(kāi)源解決方案。zabbix能監(jiān)視各種網(wǎng)絡(luò)參數(shù),保證服務(wù)器系統(tǒng)的安全運(yùn)營(yíng);本文講述的是使用playbook安裝zabbix客戶(hù)端。2014-07-07
ubuntu20.04安裝unity-tweak-tools啟動(dòng)時(shí)遇到錯(cuò)誤的解決
在Ubuntu系統(tǒng)中,安裝Unity Tweak Tool時(shí)可能會(huì)遇到schemacom.canonical.Unity.ApplicationsLens未安裝的錯(cuò)誤,解決這個(gè)問(wèn)題的辦法是安裝缺失的依賴(lài)包,執(zhí)行命令`sudo apt-get install unity-lens-applications` 和 `sudo apt-get install unity-lens-files`2024-09-09
Apache Hudi結(jié)合Flink的億級(jí)數(shù)據(jù)入湖實(shí)踐解析
這篇文章主要為大家介紹了Apache Hudi結(jié)合Flink的億級(jí)數(shù)據(jù)入湖實(shí)踐解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步早日升職加薪2022-03-03
Hadoop計(jì)數(shù)器的應(yīng)用以及數(shù)據(jù)清洗
今天小編就為大家分享一篇關(guān)于Hadoop計(jì)數(shù)器的應(yīng)用以及數(shù)據(jù)清洗,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2019-01-01

