Connect 中的响应重定向

问题描述 投票:0回答:2

在 Express 中,我可以使用 response.redirect("") 重定向到其他 url。同样,我如何在 Connect 模块中重定向?我已经尝试过下面的代码,但它不起作用。

response.setHeader('Content-Type', 'text/plain');
response.end('<p>302. Redirecting to <a href="' + url+ '">' + url+ '</a></p>');
node.js express response.redirect node.js-connect
2个回答
6
投票

您还可以使用

writeHead
在 Connect 中重定向,如下所示:

res.writeHead(301, {Location: url});
res.end();

301 http状态代码的意思是“永久移动”。


0
投票

res.redirect
是在express中定义的,而不是在connect中定义的(参见相关源代码)。然后您将无法使用此函数,但您可以通过设置
Location
标头来复制其行为:

res.set('Location', url);

您还可以阅读这个答案:即使与相关,它也包含有关标头使用的有用信息。

© www.soinside.com 2019 - 2024. All rights reserved.