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://yangxiang.fun/'});
    res.end();
}).listen(80);

  


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM