
No more than code.
新增代码 1 :
// [注意放在 path 变量定义后]
const https = require('https');
const fs = require('fs');
var privateKey = fs.readFileSync(path.join(__dirname, './cert/private.pem'), 'utf8');
var certificate = fs.readFileSync(path.join(__dirname, './cert/file.crt'), 'utf8');
var credentials = {key: privateKey, cert: certificate};
新增代码 2 :
在 const port = process.env.PORT || config.dev.port
后面添加一行
const httpsPort = process.env.httpsPort || config.dev.httpsPort
注意:config/index.js dev 对象加入 httpsPort: process.env.PORT || 自定义端口号
新增代码 3 :
在 server = app.listen(port)
后面加入以下代码开启https服务:
server = app.listen(port);
var httpsServer = https.createServer(credentials, app);
httpsServer.listen(httpsPort, function() {
console.log('HTTPS Server is running on: https://localhost:%s', httpsPort);
});
openssl genrsa 1024 > ./build/cert/private.pem
openssl req -new -key ./build/cert/private.pem -out csr.pem
openssl x509 -req -days 365 -in csr.pem -signkey ./build/cert/private.pem -out ./build/cert/file.crt
此时 build/cert 目录下生成两个文件 private.pem/file.crt