我正在使用express在node.js中制作一个web应用程序。这是对我所拥有的东西的简化:

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中发送的数据不有效,我想向/页传递一些额外的上下文。我怎么能这样做呢?重定向似乎不允许任何类型的额外参数。


当前回答

你可以通过查询字符串传递少量的键/值对数据:

res.redirect('/?error=denied');

主页上的javascript可以访问它并相应地调整它的行为。

注意,如果你不介意/category作为浏览器地址栏中的URL,你可以直接渲染而不是重定向。恕我直言,很多时候人们使用重定向,因为旧的web框架使直接响应变得困难,但表达起来很容易:

app.post('/category', function(req, res) {

  // Process the data received in req.body

  res.render('home.jade', {error: 'denied'});
});

@Dropped.on。Caprica评论说,使用AJAX消除了URL更改问题。

其他回答

使用app.set & app.get

设置数据

router.get(
  "/facebook/callback",
  passport.authenticate("facebook"),
  (req, res) => {
    req.app.set('user', res.req.user)
    return res.redirect("/sign");
  }
);

获取数据

router.get("/sign", (req, res) => {
  console.log('sign', req.app.get('user'))
});

 app.get('/category', function(req, res) {
  var string = query
  res.redirect('/?valid=' + string);
});

在ejs中,你可以直接使用valid:

<% var k = valid %>

下面是我的建议,不使用任何其他依赖,只是节点和表达式,使用app.locals,这是一个例子:

    app.get("/", function(req, res) {
        var context = req.app.locals.specialContext;
        req.app.locals.specialContext = null;
        res.render("home.jade", context); 
        // or if you are using ejs
        res.render("home", {context: context}); 
    });

    function middleware(req, res, next) {
        req.app.locals.specialContext = * your context goes here *
        res.redirect("/");
    }

我用了一个非常简单但有效的技巧 在我的app.js(我的入口点) 我定义了一个变量

let authUser = {};

然后我分配给它从我的路由页面(像成功登录后)

authUser = matchedUser

这可能不是最好的方法,但它符合我的需求。

我不得不寻找另一个解决方案,因为所提供的解决方案实际上没有一个满足我的要求,原因如下:

Query strings: You may not want to use query strings because the URLs could be shared by your users, and sometimes the query parameters do not make sense for a different user. For example, an error such as ?error=sessionExpired should never be displayed to another user by accident. req.session: You may not want to use req.session because you need the express-session dependency for this, which includes setting up a session store (such as MongoDB), which you may not need at all, or maybe you are already using a custom session store solution. next(): You may not want to use next() or next("router") because this essentially just renders your new page under the original URL, it's not really a redirect to the new URL, more like a forward/rewrite, which may not be acceptable.

所以这是我的第四个解决方案,没有任何前面的问题。基本上,它涉及到使用一个临时cookie,为此你必须首先安装cookie-parser。显然,这意味着它只能在启用cookie的情况下工作,并且数据量有限。

实现的例子:

var cookieParser = require("cookie-parser");

app.use(cookieParser());

app.get("/", function(req, res) {
    var context = req.cookies["context"];
    res.clearCookie("context", { httpOnly: true });
    res.render("home.jade", context); // Here context is just a string, you will have to provide a valid context for your template engine
});

app.post("/category", function(req, res) {
    res.cookie("context", "myContext", { httpOnly: true });
    res.redirect("/");
}