Skip to content

创建https服务

配置如下

js
const express = require('./node_modules/express')
const app = express()
const path = require('path')
const fs = require('fs')
var https  = require('https') // 一、引入https模块
const port = process.env.PORT || 3003

// 二、同步读取密钥和签名证书(腾讯云的免费证书文件)
var options = {
    key: fs.readFileSync('../keys/2_chaoszhu.com.key'),
    cert: fs.readFileSync('../keys/1_chaoszhu.com_bundle.crt')
}
var httpsServer = https.createServer(options, app) // 三、创建服务
httpsServer.listen(3002, ()=>{ // 四、监听端口
    console.log(`https服务启动成功:https://localhost:3002`)
})
app.listen(port, () => {
    console.log('http服务启动成功:http://localhost:3003')
})