在通过某些上下文时如何重定向到expressjs? [英] How do I redirect in expressjs while passing some context?

查看:119
本文介绍了在通过某些上下文时如何重定向到expressjs?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用express在node.js中制作一个网络应用程序。这是一个简单的例子:

I am using express to make a web app in node.js. This is a simplification of what I have:

var express = require('express');
var jade = require('jade');
var http = require("http");


var app = express();
var server = http.createServer(app);

app.get('/', function(req, res) {
    // Prepare the context
    res.render('home.jade', context);
});

app.post('/category', function(req, res) {
    // Process the data received in req.body
    res.redirect('/');
});

我的问题如下:

如果我发现在 / category 中发送的数据不能验证,我想传递一些额外的上下文到 / 页面。我该怎么做?重定向似乎不允许任何种类的额外参数。

If I find that the data sent in /category doesn't validate, I would like pass some additional context to the / page. How could I do this? Redirect doesn't seem to allow any kind of extra parameter.

推荐答案

有几种方法可以将数据传递到不同的路由。最正确的答案当然是查询字符串。您需要确保值正确 encodeURIComponent decodeURIComponent

There are a few ways of passing data around to different routes. The most correct answer is, of course, query strings. You'll need to ensure that the values are properly encodeURIComponent and decodeURIComponent.

app.get('/category', function(req, res) {
  var string = encodeURIComponent('something that would break');
  res.redirect('/?valid=' + string);
});

您可以通过使用发送参数来阻止您的其他路由req.query

app.get('/', function(req, res) {
  var passedVariable = req.query.valid;
  // Do something with variable
});

对于更动态的方式,您可以使用 url 核心模块为您生成查询字符串:

For more dynamic way you can use the url core module to generate the query string for you:

const url = require('url');    
app.get('/category', function(req, res) {
    res.redirect(url.format({
       pathname:"/",
       query: {
          "a": 1,
          "b": 2,
          "valid":"your string here"
        }
     }));
 });

所以如果你想重定向所有req查询字符串变量,你可以简单地做

So if you want to redirect all req query string variables you can simply do

res.redirect(url.format({
       pathname:"/",
       query:req.query,
     });
 });

如果您使用Node> = 7.x,还可以使用 querystring 核心模块

And if you are using Node >= 7.x you can also use the querystring core module

const querystring = require('querystring');    
app.get('/category', function(req, res) {
      const query = querystring.stringify({
          "a": 1,
          "b": 2,
          "valid":"your string here"
      });
      res.redirect('/?' + query);
 });

另一种做法是在会话中设置一些东西。 您可以阅读如何在此设置,但设置和访问变量是这样的:

Another way of doing it is by setting something up in the session. You can read how to set it up here, but to set and access variables is something like this:

app.get('/category', function(req, res) {
  req.session.valid = true;
  res.redirect('/');
});

稍后重定向...

app.get('/', function(req, res) {
  var passedVariable = req.session.valid;
  req.session.valid = null; // resets session variable
  // Do something
});

还可以选择使用Express的旧功能, req。闪光。在较新版本的Express中执行此操作需要您使用另一个库。本质上,它允许您设置变量,在下一次访问页面时将显示并重置。显示错误给用户非常方便,但是默认情况下也会被删除。编辑:找到一个添加此功能的库

There is also the option of using an old feature of Express, req.flash. Doing so in newer versions of Express will require you to use another library. Essentially it allows you to set up variables that will show up and reset the next time you go to a page. It's handy for showing errors to users, but again it's been removed by default. Found a library that adds this functionality.

希望这将给你一个一般的想法如何传递在Express应用程序中的信息。

Hopefully that will give you a general idea how to pass information around in an Express application.

这篇关于在通过某些上下文时如何重定向到expressjs?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆