Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9163331
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T14:25:18+00:00 2026-06-17T14:25:18+00:00

This is my Express middleware stack: var server = express() .use(express.cookieParser()) .use(express.session({secret: ‘Secret’})) .use(express.bodyParser())

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T14:25:19+00:00Added an answer on June 17, 2026 at 2:25 pm

    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);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is my Express configuration code: var server = express() .use(express.cookieParser()) .use(express.session({secret: buffer.toString('hex')})) .use(express.bodyParser())
I have a basic Socket.io server setup like this: var server = express.createServer().listen(port); this.io
I'm using node.js and the less compiler middleware: app.configure(function() { // ... app.use(express.compiler({ src:
Why does SQL server express 2008 give me this error? CREATE TABLE model (
I have a express route like this: app.get('/', auth.authOrDie, function(req, res) { res.send(); });
In express, I can something like this to have a static server, with directory
this is a simple question... how can i use formidable instead bodyparser()... how to
With Express / Connect I can set up an middleware function in either of
Almost every Express app I see has an app.use statement for middleware but I
after install Express.js, I wrote this on node: var express = require('express'), app =express.createServer();

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.