关于如何生成签名文件可以参考我的文章:传送门
中的 OpenSSL 安装 与 生成SSL证书 段。
node相关的代码如下:
var express = require('express');
var https = require('https');
var http = require('http');
var fs = require('fs');
//同步读取密钥和签名证书
var options = {
key:fs.readFileSync('./keys/server.key'),
cert:fs.readFileSync('./keys/server.crt')
}
var app = express();
var httpsServer = https.createServer(options,app);
var httpServer = http.createServer(app);
app.get('/',function(req,res,next){
res.send('Hello Express+https');
console.log("Hello Express+https");
});
//https监听3000端口
httpsServer.listen(3000);
//http监听3001端口
httpServer.listen(3001);