记录如下:
1、申请证书
2、修改配置如下:
var express = require('express');
var fs = require('fs');
var http = require('http');
var https = require('https');
var privateKey = fs.readFileSync('/repo/ssl/8996123_www.xxx.cn.key', 'utf8');
var certificate = fs.readFileSync('/repo/ssl/8996123_www.xxx.cn.crt', 'utf8');
var credentials = {key: privateKey, cert: certificate};
var app = express();
app.use("/", (req, res, next) => {
if(req.protocol === 'http'){
// console.log('HTTP Server is running on port', `${req.headers.host}${req.url}`);
res.redirect(301, `https://${req.headers.host}${req.url}`)
}
next()
});
app.use('/',express.static(__dirname + "/public"));
var httpServer = http.createServer(app);
var httpsServer = https.createServer(credentials, app);
var PORT = 80;
var SSLPORT = 443;
httpServer.listen(PORT, function() {
console.log('HTTP Server is running on port', PORT);
});
httpsServer.listen(SSLPORT, function(){
console.log('Express https server listening on port ' + SSLPORT);
});