Nodejs:重定向 URL

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

我正在尝试以这种方式在 node.js 中重定向我的应用程序的 URL:

I'm trying to redirect the url of my app in node.js in this way:

// response comes from the http server response.statusCode = 302; response.setHeader("Location", "/page"); response.end();

但是当前页面和新页面混在一起,看起来很奇怪:|我的解决方案看起来完全合乎逻辑,我真的不知道为什么会发生这种情况,但是如果我在重定向后重新加载页面,它会起作用.

But the current page is mixed with the new one, it looks strange :| My solution looked totally logical, I don't really know why this happens, but if I reload the page after the redirection it works.

无论如何,在节点中进行 HTTP 重定向的正确方法是什么?

Anyway what's the proper way to do HTTP redirects in node?

推荐答案

看起来 express 和你的方式差不多.从我所看到的不同之处在于它们推送一些正文内容并使用绝对网址.

Looks like express does it pretty much the way you have. From what I can see the differences are that they push some body content and use an absolute url.

查看 express response.redirect 方法:

See the express response.redirect method:

github/visionmedia/express/blob/master/lib/response.js#L335

// Support text/{plain,html} by default if (req.accepts('html')) { body = '<p>' + http.STATUS_CODES[status] + '. Redirecting to <a href="' + url + '">' + url + '</a></p>'; this.header('Content-Type', 'text/html'); } else { body = http.STATUS_CODES[status] + '. Redirecting to ' + url; this.header('Content-Type', 'text/plain'); } // Respond this.statusCode = status; this.header('Location', url); this.end(body); };

更多推荐

Nodejs:重定向 URL

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

发布评论

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

>www.elefans.com

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