node.js res.redirect('back') 带参数

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

res.redirect('back') with parameters

node.jsredirectexpress

提问by f1nn

I have a register form on every page of my website. During registration some error may occur. After catching the error, I have to return the user to the previous page, showing some error message. The problem is that I do not know from which page the user performed the registration, so I use res.redirect('back');. However, I cannot just redirect user back, I have to display the error message also, so I have to pass some argument. But res.redirect('back', (reg_error:'username')})cannot be used directly because res.redirect()does not support parameters. How can I render the previous page with some parameter?

我网站的每个页面上都有一个注册表。在注册过程中可能会出现一些错误。捕获错误后,我必须将用户返回到上一页,显示一些错误消息。问题是我不知道用户是从哪个页面进行注册的,所以我使用res.redirect('back');. 但是,我不能只是将用户重定向回来,我还必须显示错误消息,因此我必须传递一些参数。但res.redirect('back', (reg_error:'username')})不能直接使用,因为res.redirect()不支持参数。如何使用某些参数呈现上一页?

回答by Plato

Using the referer header to find what page your user came from might be helpful:

使用引用标头查找您的用户来自哪个页面可能会有所帮助:

app.get('/mobileon', function(req, res){
  backURL=req.header('Referer') || '/';
  // do your thang
  res.redirect(backURL);
});

You might also want to store backURLin req.session, if you need it to persist across multiple routes. Remember to test for the existence of that variable, something like: res.redirect(req.session.backURL || '/')

backURL如果您需要它在多个路由中持续存在,您可能还想存储在 req.session 中。请记住测试该变量是否存在,例如:res.redirect(req.session.backURL || '/')



edit:Since my answer has been getting some upvotes I typed up my current way to do this. It got a little long so you can view it at https://gist.github.com/therealplato/7997908.

编辑:由于我的回答得到了一些赞成,所以我输入了我目前的做法。它有点长,所以你可以在https://gist.github.com/therealplato/7997908查看。

The most important difference is using an explicit 'bounce' parameter in the query string that overrides the Referer url.

最重要的区别是在覆盖 Referer url 的查询字符串中使用显式“bounce”参数。

回答by Jed Watson

A really easy way of implementing this is to use connect-flash, a middleware for express that leverages the session middleware built into express to pass 'flash' messages to the next request.

实现这一点的一个非常简单的方法是使用connect-flash,这是express 的中间件,它利用 express 中内置的会话中间件将“flash”消息传递给下一个请求。

It means you don't need to add fields to track urls or messages to the form or route patterns for your views.

这意味着您不需要添加字段来跟踪视图的表单或路由模式的 url 或消息。

It provides a simple version of the same feature in Ruby on Rails.

它提供了Ruby on Rails 中相同功能的简单版本。

(obviously you have to be using the Express frameworkfor this to use, but I highly recommend that too!)

(显然,您必须使用Express 框架才能使用它,但我也强烈建议您这样做!)

Plug it in like this:

像这样插入:

var flash = require('connect-flash');
var app = express();

app.configure(function() {
  app.use(express.cookieParser('keyboard cat'));
  app.use(express.session({ cookie: { maxAge: 60000 }}));
  app.use(flash());
});

And then use it like this:

然后像这样使用它:

app.get('/flash', function(req, res){
  req.flash('info', 'Flashed message')
  res.redirect('/');
});

app.get('/', function(req, res){
  res.render('index', { message: req.flash('info') });
});

回答by gadu

You could simply have it redirect as res.redirect('..?error=1')

您可以简单地将其重定向为 res.redirect('..?error=1')

the ?tag tells the browser that it is a set of optional parameters and the ..is just a pathname relative recall (like calling cd ..on terminal to move back one directory) and your browser will direct to the appropriate page with that tag at the end: http://.....?error=1

?标签告诉浏览器它是一组可选参数,..它只是一个路径名相对召回(如调用cd ..终端移回一个目录),您的浏览器将定向到带有该标签的相应页面:http://.....?error=1

then you can simply pull the error on the appropriate page by doing a:

然后您可以通过执行以下操作简单地将错误拉到适当的页面上:

if (req.param("error" == 1)) { 
// do stuff bassed off that error match
};

you can hardcode in several different error values and have it respond appropriately depending on what error occurred

您可以对几个不同的错误值进行硬编码,并根据发生的错误做出适当的响应

Note that this makes your webpage susceptible to manipulation since users can type in their own parameters and get your page to respond to it, so make sure the action you take is not something you wouldn't want people abusing. (Should be fine for simple error displaying)

请注意,这会使您的网页容易受到操纵,因为用户可以输入他们自己的参数并让您的页面对其做出响应,因此请确保您采取的操作不是您不希望人们滥用的。(对于简单的错误显示应该没问题)

回答by danmactough

If you're using sessions, you can just add that reg_errorobject to the req.sessionobject before your redirect. Then it will be available on the req.sessionobject when you're loading the previous page.

如果您正在使用会话,则可以在重定向之前reg_error将该req.session对象添加到该对象中。然后,req.session当您加载上一页时,它将在对象上可用。

回答by Angel Quiroz Soriano

you can use connect-flash package to send a error or successfull message.

您可以使用 connect-flash 包发送错误或成功消息。

https://www.npmjs.com/package/connect-flash

https://www.npmjs.com/package/connect-flash

回答by saeed

You should add page parameter to the form. Then you can read it on the server and use it for the redirect.

您应该将页面参数添加到表单中。然后您可以在服务器上读取它并将其用于重定向。

res.redirect(page, {reg_error:'username'}) // page being the url to redirect to.