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