node.js 如何在 Express.js 中重新加载当前页面?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15825333/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 14:07:03  来源:igfitidea点击:

How to reload current page in Express.js?

node.jsexpress

提问by nfpyfzyf

I'm current in /id/1234page, and click a POST /addbutton, after that I want to reload /id/1234in Experss.js:res.redirect(**How to get /id/1234 dynamicly, since I will be in /id/1235, /id/1236 page do a same POST /add action?**)

我当前在/id/1234页面中,然后单击一个POST /add按钮,然后我想/id/1234在 Experss.js 中重新加载:res.redirect(**How to get /id/1234 dynamicly, since I will be in /id/1235, /id/1236 page do a same POST /add action?**)

Thanks.

谢谢。

回答by Sean Dawson

I'd just like to add that in the version 4.x of Express you can use

我只想在您可以使用的 Express 4.x 版中添加

res.redirect('back');

to automatically redirect back to the page the request came from. This is the equivilant of

自动重定向回请求来自的页面。这是等价的

res.redirect(req.get('referer'));

that is mentioned in Peter Lyons answer

Peter Lyons 的回答中提到

See also: http://expressjs.com/api.html#res.redirect

另见:http: //expressjs.com/api.html#res.redirect

回答by Peter Lyons

To give you the answer you probably want, the browser will send a header called [Referer][1]which will have the URL of the /id/1234page, so do:

为了给你你可能想要的答案,浏览器将发送一个名为的标题[Referer][1],其中包含/id/1234页面的 URL ,所以这样做:

res.redirect(req.get('referer'));

However, your URL path design is probably poor if you need to do this. Better options might be submitting the form via AJAX without changing the URL or including the 1234id in the form body and using that value from the POST request to generate the correct URL for the corresponding redirect.

但是,如果您需要这样做,您的 URL 路径设计可能很差。更好的选择可能是通过 AJAX 提交表单而不更改 URL 或1234在表单正文中包含id,并使用来自 POST 请求的该值为相应的重定向生成正确的 URL。

回答by Gavin Gong

I use express 4.x you can write like this

我用express 4.x 你可以这样写

res.redirect(req.originalUrl)

回答by Brandon Buck

res.redirect("/id/1234")

You just give the path (or full URI) that you wish to redirect to.

您只需提供您希望重定向到的路径(或完整 URI)。

And if you're id is in the post data:

如果您的 id 在帖子数据中:

res.redirect("/id/" + req.body.id);