1.使用以下命令行生成证书
执行命令行后,会在当前文件夹下生成三个文件
openssl genrsa -out key.pem
openssl req -new -key key.pem -out csr.pem
openssl x509 -req -days 9999 -in csr.pem -signkey key.pem -out cert.pem
2.创建https服务器
var https = require('https');
var fs = require('fs');
var server_port = 8080;
var httpsOptions = {
key: fs.readFileSync('\key.pem'),
cert: fs.readFileSync('\cert.pem')
};
var app = function (req, res) {
res.writeHead(200);
res.end("hello world\n");
}
https.createServer(httpsOptions, app).listen(server_port);
3.在浏览器中打开:https://localhost:8080