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);

  

posted @ 2020-04-29 17:27  前端大兵  阅读(1452)  评论(0编辑  收藏  举报