WishMeLz

生活其实很有趣

Nodejs express搭建https服务

阿里云申请ssl证书。

下载证书 就出现两个文件 .key与.pem文件

var fs = require('fs');
var http = require('http');
var https = require('https');
var privateKey  = fs.readFileSync('./path/**.key', 'utf8');
var certificate = fs.readFileSync('./path/**.pem', 'utf8');
var credentials = {key: privateKey, cert: certificate};
const app = require('express')();
var httpServer = http.createServer(app);
var httpsServer = https.createServer(credentials, app);
httpsServer.listen('8901',() => {
    console.log("server into run http://127.0.0.1:8901");
})