javascript 在 Express 中 res.render() 和 res.redirect() 的正确用法是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13795197/
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
What is the proper usage of res.render() and res.redirect() in Express?
提问by netpoetica
I'm having some trouble deciphering the ambiguity between
我在解读两者之间的歧义时遇到了一些麻烦
res.render('viewname', {msg: 'Message' })
and
和
res.redirect('route')
The redirect function does not let you pass a "message", but you may still want to, and the render function will render your view, but it will not change the URL of your web app and will not trigger the function which is required for your route.
重定向功能不允许您传递“消息”,但您可能仍然想要,并且渲染功能将呈现您的视图,但它不会更改您的网络应用程序的 URL,也不会触发所需的功能你的路线。
The situation I am having is that I have an Invite form, which has an action that changes my URL, and triggers a function at that route which has a success and failure callback, and I would like to redirect users to the Dashboard with a message indicating success or failure.
我遇到的情况是我有一个 Invite 表单,它有一个更改我的 URL 的操作,并在该路由上触发一个函数,该函数具有成功和失败回调,我想通过一条消息将用户重定向到仪表板表示成功或失败。
回答by chovy
look at connect-flash to use rails style flashing of messages.
查看 connect-flash 以使用 rails 样式的消息闪烁。
res.render() will render the view with data passed to it, res.redirect() will redirect a user to another page (at which point the request starts over)
res.render() 将使用传递给它的数据渲染视图, res.redirect() 将用户重定向到另一个页面(此时请求重新开始)