Node啟動(dòng)https服務(wù)器的教程
首先你需要生成https證書(shū),可以去付費(fèi)的網(wǎng)站購(gòu)買或者找一些免費(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)文章
網(wǎng)站下載文件時(shí) 地址加jdfwkey=的說(shuō)明
因WEB服務(wù)器前置硬防,地址加?jdfwkey=影響收錄的解決辦法2009-12-12服務(wù)器常用磁盤(pán)陣列RAID原理、種類及性能優(yōu)缺點(diǎn)對(duì)比
這篇文章主要介紹了磁盤(pán)陣列RAID原理、種類及性能優(yōu)缺點(diǎn)對(duì)比,根據(jù)硬件與硬盤(pán)數(shù)量選擇適合自己的磁盤(pán)陣列很重要,需要的朋友可以參考下2018-05-05Win2003下cwRsyncServer服務(wù)端與cwRsync客戶端數(shù)據(jù)同步實(shí)例教程
這篇文章主要介紹了Win2003下cwRsyncServer服務(wù)端與cwRsync客戶端數(shù)據(jù)同步實(shí)例教程,需要的朋友可以參考下2015-07-07