NodeJS + Express - 将会话中间件应用于某些路由 [英] NodeJS + Express - Apply session middleware to some routes

查看:89
本文介绍了NodeJS + Express - 将会话中间件应用于某些路由的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Express应用程序有一些路由,只有两个需要支持会话。
我读到无处不在的中间件定义(app.use(express.session({...)仅适用于之后的路由,所以我创建了这个示例:

  var express = require('express'); 
var app = express();
app.use(express.bodyParser()) ;

app.get('/ path1',function(req,res){
res.send('text response');
});


app.use(express.cookieParser());
app.use(express.session({
secret:'secret',
cookie:{maxAge :new Date(Date.now()+ 2 * 60 * 1000)}
}));


app.get('/ path2',function(req, res){
res.session.test = {test:test};
res.send('text response');
});

app.listen(8088);

但这不起作用:in / path2 res。会话未定义。

如果我移动会话中间件定义 - 一切正常,但是我看到在调用/ path1(th)时正在创建会话是我想避免的)



有人可以解释一个单个应用程序只能使用某些路由



谢谢!



/////更新//////

经过更多的挖掘 - 我想出了:



不要使用:应用程序。使用(express.session({...}));

相反 - 定义以下内容:

  var sessionMiddleware = express.session({
//会话配置
});

函数sessionHandler(req,res,next){sessionMiddleware(req,res,next); }

然后在需要会话支持的特定路由上应用处理程序:

  app.get('/ path_that_need_session',sessionHandler,function(req,res){
/ *使用req.session * /
});


解决方案

不要使用 .use(express.session({...}))



相反,初始化会话中间件,保存对结果的引用函数然后直接包含在您想要的路径上。

  var express = require('express'),
app = express();

var session = express.session({
//会话配置
});

app.use(app.router);


//必须来自app.use(app.router);
app.get('/ your / route / here',session,function(req,res){
//路由处理程序的代码在这里
});

比更新中包含的答案稍微简单一些(你不需要包装器功能) / p>

I have an Express application with some routes, only two of them need to support sessions. I read everywhere that the middleware definition (app.use(express.session({...) applies only to the routes that comes after it, so I created this sample:

var express = require('express');
var app = express();
app.use(express.bodyParser());

app.get('/path1', function (req, res) {
    res.send('text response');
});


app.use(express.cookieParser());
app.use(express.session({
    secret: 'secret',
    cookie: { maxAge: new Date(Date.now() + 2 * 60 * 1000) }
}));


app.get('/path2', function (req, res) {
    res.session.test = { "test": "test" };
    res.send('text response');
});

app.listen(8088);

But this doesn't work: in /path2 res.session is undefined.
If I move the session middleware definition up - everything works, but I see that sessions are being create when calling /path1 (this is what I want to avoid)

Can someone explain how a single application can use session in only some of the routes.

Thanks!

///// UPDATE //////

After more digging - I figured it out:

Don't use: app.use(express.session({ ... }));
Instead - define the following:

var sessionMiddleware = express.session({
    //session configurations
});

function sessionHandler(req, res, next) { sessionMiddleware(req, res, next); }

Then apply the handler on the specific route/s that need session support:

app.get('/path_that_need_session', sessionHandler, function (req, res) {                     
 /* Do somthing with req.session */  
});

解决方案

Don't use app.use(express.session({ ... })).

Instead, initialize the session middleware, save a reference to the resulting function then include it directly in the routes you want it on.

var express = require('express'),
    app = express();

var session = express.session({
    //session configuration
});

app.use(app.router);


// must come after app.use(app.router);
app.get('/your/route/here', session, function(req, res){
    // code for route handler goes here
});

Slightly simpler than the answer included in the update (you don't need the wrapper function).

这篇关于NodeJS + Express - 将会话中间件应用于某些路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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