将4.x重定向到http

编程入门 行业动态 更新时间:2024-10-26 16:29:18
本文介绍了将4.x重定向到http的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有以下代码:

var https = require('https'); var http = require('http'); var express = require('express'); var app = express(); var router = express.Router(); app.use('/', router); //listen server on https var server = https.createServer(config.sslCredential, app); server.listen(config.serverPort); //listen server on http, and always redirect to https var httpServer = http.createServer(function(req,res){ res.redirect(config.serverDomain+req.url); }); httpServer.listen(config.httpServerPort);

但不知何故,我无法将https请求重定向到https请求,我该怎么办正确使用express 4.x的node.js?

but somehow i can't get https request to be redirected into https request, how should i do this correctly on node.js with express 4.x ?

推荐答案

从我自己的答案(其中btw是on express 3.0)

Quoting a middleware solution from my own answer (which btw was on express 3.0)

app.all('*', ensureSecure); // at top of routing calls http.createServer(app).listen(80) https.createServer(sslOptions, app).listen(443) function ensureSecure(req, res, next){ if(req.secure){ // OK, continue return next(); }; // handle port numbers if you need non defaults // res.redirect('' + req.host + req.url); // express 3.x res.redirect('' + req.hostname + req.url); // express 4.x }

更多推荐

将4.x重定向到http

本文发布于:2023-11-17 17:10:34,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1610412.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:重定向   http

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!