node - http 重定向到 https

node - http 重定向到 https


const https = require('https');
const http = require('http');
const fs = require('fs');
  
// 读取证书文件
const options = {
  key: fs.readFileSync('/root/project/cert/3762675_yangxiang.fun.key'),
  cert: fs.readFileSync('/root/project/cert/3762675_yangxiang.fun.pem')
};
  
// 创建https服务
const server = https.createServer(options, function(req, res) {
  res.writeHead(200, {'Content-Type': 'text/plain'});
  res.end('hello world');
});
server.listen(443);

// 创建http服务,重定向到https
http.createServer((req,res)=>{
  res.writeHead(301, {'Location': 'https://your-domain.com'});
    res.end();
}).listen(80);


Ref

posted @ 2022-11-01 11:22  炎黄子孙,龙的传人  阅读(23)  评论(0编辑  收藏  举报