javascript - Access session object in Express middleware - Stack Overflow

This is my Express middleware stack:var server = express().use(express.cookieParser()).use(express.sess

This is my Express middleware stack:

var server = express()
    .use(express.cookieParser())
    .use(express.session({secret: 'Secret'}))
    .use(express.bodyParser())
    .use(function printSession(req, res, next) {
        console.log(req.session.user);
        next();
    })
    .use(express.static('./../'));

and here are two routes:

server.post('/setSession', function (req, res) {
    req.session.user = 'admin';
}

server.post('/getSession', function (req, res) {
    console.log(req.session.user);
}

Now the session management in the route handlers work find. I can set session.user and it will persist for the subsequent requests in the same session, as confirmed by getSession. However, the middleware function printSession always prints undefined.

How can I access the populated session object in the middleware?

This is my Express middleware stack:

var server = express()
    .use(express.cookieParser())
    .use(express.session({secret: 'Secret'}))
    .use(express.bodyParser())
    .use(function printSession(req, res, next) {
        console.log(req.session.user);
        next();
    })
    .use(express.static('./../'));

and here are two routes:

server.post('/setSession', function (req, res) {
    req.session.user = 'admin';
}

server.post('/getSession', function (req, res) {
    console.log(req.session.user);
}

Now the session management in the route handlers work find. I can set session.user and it will persist for the subsequent requests in the same session, as confirmed by getSession. However, the middleware function printSession always prints undefined.

How can I access the populated session object in the middleware?

Share Improve this question edited Jan 19, 2013 at 12:43 Randomblue asked Jan 19, 2013 at 12:12 RandomblueRandomblue 116k150 gold badges363 silver badges558 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 3

This program works fine. Before I access /setSession, the middleware prints after session: undefined. Once I GET /setSession, it prints after session: admin. As long as the browser you are testing with (not curl) stores and sends the session cookies, this will work as expected.

var express = require('express');
var server = express();
server.use(express.cookieParser());
server.use(express.session({secret: 'SEKRET'}));
server.use(function (q,r,n) {console.log('after session:', q.session.user);n();});
server.get('/', function (q,r,n) {r.send("you got slashed");});
server.get('/setSession', function (req, res) {
  console.log("logging admin in via /setSession");
  req.session.user = 'admin';
  res.send("admin logged in");
});
server.listen(3000);

There must be something wrong with your settings. The following example, that is very similar to your code but uses GET instead POST, works fine for me

app.configure(function(){
  // ...
  app.use(express.cookieParser('your secret here'));
  app.use(express.session());

  app.use(function(req, res, next) {
    console.log(req.session.user + ' from middleware');
    next();
  });

  app.use(app.router);
  app.use(express.static(path.join(__dirname, 'public')));
});

and

app.get('/getSession', function(req, res) {
  console.log(req.session.user);
  res.send('awesome');
});

app.get('/setSession', function(req, res) {
  req.session.user = 'admin';
  res.send('done');
});

Now when you do the following everything works as expected

  1. GET /getSession => undefined from middleware, undefined
  2. GET /setSession => undefined from middleware
  3. GET /getSession => admin from middleware, admin

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744845701a4596822.html

相关推荐

  • javascript - Access session object in Express middleware - Stack Overflow

    This is my Express middleware stack:var server = express().use(express.cookieParser()).use(express.sess

    2天前
    60

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信