实测有用node.js/express从HTTP重定向到HTTPS

'use strict';
const express = require('express')
const http = require('http')
const https = require('https')
const fs = require('fs')
const path = require('path')
const domain = 'xxxx.com'
const app = express()
app.get('*',function(req, res){
    res.redirect('https://' + domain + req.path);
});
app.use(express.static(path.join(__dirname,'public')))

http.createServer(app).listen(80, function(){
    console.log('HTTP listening on port 80 33');
});

const appSecure = express();
appSecure.use(express.static(path.join(__dirname,'public')))
var options = {
  key: fs.readFileSync('/mydata/key/1.key'),
  cert: fs.readFileSync('/mydata/key/1.crt'),
};
https.createServer(options, appSecure).listen(443, function(){
    console.log('HTTPS listening on port 443');
});

服务器正常运行的,脱敏后的代码

微信公众号
手机浏览(小程序)
0
分享到:
没有账号? 忘记密码?