Javascript 如何在 node.js 中重定向

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11739715/
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-08-24 06:54:44  来源:igfitidea点击:

how to redirect in node.js

javascriptnode.jsredirectwebexpress

提问by nihulus

I'm having some issues with redirecting between directories, the problem is I can manage to redirect to another file in different directory. My directory structure looks like this:

我在目录之间重定向时遇到了一些问题,问题是我可以设法重定向到不同目录中的另一个文件。我的目录结构如下所示:

-views
  -add_user.jade

-routes
  -index.js

I try to redirect to add_ user.jadefrom index.js, how would you guys do it

我尝试重定向以从index.js添加_ user.jade,你们会怎么做

res.redirect('???');


If index.jsin the same directory as views, the code below works

如果index.js在与views相同的目录中,则下面的代码有效

-index.js
-views
  -add_user.jade

res.redirect('./add_users');

回答by Hector Correa

You want to redirect to the URL(not the view name) that you want the user to go to.

您希望重定向到您希望用户访问的URL(而不是视图名称)。

For example, if the route "/user/add"renders the "add_user.jade"view then you want to use

例如,如果路由"/user/add"呈现"add_user.jade"视图,那么您要使用

 res.redirect("/user/add");

回答by Rahul Dhiman

Yes, you can redirect on Url with the help of

是的,您可以在 Url 的帮助下重定向

return res.redirect('/server/Thank'); 

Thank is a view file with Thank.jadeextension.

感谢是带有Thank.jade扩展名的视图文件。

and route is /server/thank

路线是 /server/thank

回答by Γε?ργιο? Βιδαλ?κη?

Try this:

尝试这个:

document.location.href="full_URL";