javascript 在 res.redirect 上传递变量

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

Passing variables on res.redirect

javascriptnode.jsexpress

提问by codephobia

Is there an easy way to pass a variable with a res.redirect in node.js / express without having to append it to the URL, or store it in a cookie, or session variable?

是否有一种简单的方法可以在 node.js / express 中传递带有 res.redirect 的变量,而无需将其附加到 URL,或将其存储在 cookie 或会话变量中?

Why do I want to do this? Here is an example:

我为什么要这样做?下面是一个例子:

Let's say I have a users page at /users. And I can add users at the page /users/add. I post the form at /users/add, if it has errors it reloads that form with the error messages, if it goes through successfully, it redirects back to the /users page. What I want is the ability to know I just added a user and show a success message for it.

假设我在 /users 有一个用户页面。我可以在页面/users/add 添加用户。我将表单发布在 /users/add,如果它有错误,它会重新加载带有错误消息的表单,如果它成功通过,它会重定向回 /users 页面。我想要的是能够知道我刚刚添加了一个用户并为其显示成功消息。

Here is an example of a post:

下面是一个帖子的例子:

exports.usersAddPost = function(req, res, next) {
  // validate form

  if (!validated) {
     // go back to form
     return res.render('users/add', req.body.whatever);
  } else {
     // go back to users list
     // need some way to tell users page that a user was just added
    return res.redirect('users');
  }
};

回答by codephobia

I actually decided to use sessions since I found a nice way to do it fairly elegantly.

我实际上决定使用会话,因为我找到了一种相当优雅的好方法。

Before the redirect I set the session like so:

在重定向之前,我像这样设置会话:

req.session['success'] = 'User added successfully';
res.redirect('users');

And on the users page I check for a success variable and pass that to my template if it exists, and then to reset it I just set it to null. This works perfectly for what I need.

在用户页面上,我检查成功变量并将其传递给我的模板(如果存在),然后重置它我只是将它设置为 null。这非常适合我的需要。

回答by Jaime Gómez

If you can use javascript, you could use the http referer and assume a user was just added, since according to your workflow that's the only way it could happen:

如果您可以使用 javascript,则可以使用 http 引用器并假设刚刚添加了一个用户,因为根据您的工作流程,这是唯一可能发生的方式:

document.referrer == "/users/add"

回答by mscdex

What about just displaying simple html with a meta refresh? Example:

只显示带有元刷新的简单 html 怎么样?例子:

function redirectFlash(url, timeout, msg) {
  var defaultTimeout = 5;
  if (typeof timeout === 'string') {
    msg = timeout;
    timeout = defaultTimeout;
  } else if (timeout === undefined) {
    msg = '';
    timeout = defaultTimeout;
  }
  return '<html><head><title>Redirecting ...</title><meta http-equiv="refresh" content="'
         + timeout
         + ';url='
         + url
         + '"></head><body>'
         + msg
         + '<br /><a href="'
         + url
         + '">Click here to return manually</a></body></html>';
}

exports.usersAddPost = function(req, res, next) {
  // validate form

  if (!validated) {
     // go back to form
     return res.render('users/add', req.body.whatever);
  } else {
     // go back to users list
     // need some way to tell users page that a user was just added
    return res.end(redirectFlash('/users', 'User added successfully. Returning you to the user list ...'));
  }
};