搭建 https 服务

首先需要将 https 证书拷贝到 node 项目中,然后添加下列代码:

const fs = require('fs')
const https = require('https')

const privateKey = fs.readFileSync('https/book_youbaobao_xyz.key', 'utf8')
const certificate = fs.readFileSync('https/book_youbaobao_xyz.pem', 'utf8')
const credentials = { key: privateKey, cert: certificate }
const httpsServer = https.createServer(credentials, app)
const SSLPORT = 18082
httpsServer.listen(SSLPORT, function() {
  console.log('HTTPS Server is running on: https://localhost:%s', SSLPORT)
})
1
2
3
4
5
6
7
8
9
10
11

启动 https 服务需要证书对象 credentials,包含了私钥和证书,重新启动 node 服务:

node app.js
1

在浏览器中输入:

https://book.youbaobao.xyz:18082
1

可以看到:

欢迎学习小慕读书管理后台
1

说明 https 服务启动成功

上次更新: 11/23/2019, 3:06:41 PM