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
标签:node,fs,http,重定向,res,https,const From: https://www.cnblogs.com/eddyz/p/16847101.html