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

