'res.location(path)'什么都不做? (表示) [英] 'res.location(path)' does not do anything? (Express)
问题描述
我一直在寻找有关 Stack Overflow 的答案;非有效...为什么res.location(path)
不起作用?我的位置标题不变.普通渲染效果很好.
I have been searching around for answers on Stack Overflow; non have worked... Why does res.location(path)
not work? My Location-header does not change. Normal rendering works fine.
我可以添加,在最终代码中,我想呈现页面.因此,我想用res.render('app', {...})
替换res.end()
并使用 handlebars.js 进行渲染.
I could add, that in the final code, I want to render the page. So, I want to replace res.end()
with res.render('app', {...})
and use handlebars.js for rendering.
代码无法正常工作:
app.get('/sub-link/:wildcard', (req, res) => {
res.location('/new-header');
res.end();
});
我一直在阅读文档;找不到原因.我发现唯一可能是问题的是浏览器:
I have been reading the documentation; could not find the reason. The only thing I found that could be the problem, is the browser:
对URL进行编码后,如果尚未进行编码,则Express会通过 在Location标头中指定浏览器的URL,没有任何 验证.
浏览器负责从获取预期的网址 当前URL或引荐URL,以及在中指定的URL 位置标头;并相应地重定向用户.
Browsers take the responsibility of deriving the intended URL from the current URL or the referring URL, and the URL specified in the Location header; and redirect the user accordingly.
- 我已使用与Firebase SDK 2018 (server) 兼容的最新Node.js进行了尝试
- 我已经在Google Chrome和IE Edge (客户端) 中进行了尝试
- I have tried this with the latest Node.js compatible with Firebase SDK 2018 (server)
- I have tried this in Google Chrome and IE Edge (client)
推荐答案
res.location
并未将响应状态代码设置为3xx或201.它仅设置了
res.location
doesn't set the response status code to 3xx or 201. It only set the Location header.
您可以使用res.redirect
代替,它将状态代码设置为302,这将使浏览器更改URL.
You can use res.redirect
instead which set the status code to 302 and will make the browser change the URL.
Location响应标头指示将页面重定向到的URL. 仅当与3xx(重定向)或201一起使用时,它才具有含义. (已创建)状态响应.
The Location response header indicates the URL to redirect a page to. It only provides a meaning when served with a 3xx (redirection) or 201 (created) status response.
这将起作用:
app.get('/sub-link', (req, res) => {
res.location('/new-header');
res.send(302);
});
或
app.get('/sub-link', (req, res) => {
res.redirect('/new-header');
});
您可以检查 res.location & ; res.redirect 代码以查看它们之间的差异
You can check res.location & res.redirect code to see the differences between them.
这篇关于'res.location(path)'什么都不做? (表示)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!