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 8644861
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T12:24:13+00:00 2026-06-12T12:24:13+00:00

I have done this before… I don’t follow what I’m doing wrong this time,

  • 0

I have done this before… I don’t follow what I’m doing wrong this time, but I’ve been struggling for a couple of hours and now consider myself mentally blocked. The corresponding code:

app.use(express.bodyParser());
app.use(i18next.handle);
app.use(express.methodOverride());
app.use(express.static(__dirname + '/public'));
app.set('views', __dirname + '/views');
app.set('view engine', 'swig');
app.set('view cache', false);
var session_store = new RedisStore({ client : redis_client});
app.use(express.errorHandler({ dumpExceptions : true, showStack : true}));
app.use(express.cookieParser());
app.use(express.session({ store : session_store, secret : SESSION_SECRET, key : "sid" }));
app.use(app.router);

Then when handling requests, here’s just an example:

app.get('/session_test', function (req, res, next) {
  console.log(req.session); //undefined
});

Connection to redis is working just fine. No errors are shown. Then, when trying to access it from the request, the req.session is undefined. The browser is sending the correct sid.

I’m no expert on the exact flow that occurs during the request, but after debugging, it seems as if the router was being called before the session middleware.

Thanks in advance for any and all the likely help. I will provide any code I can, I’m unsure what might be of your help.

Here’s more code.
server.js

  //Dependency modules
var express = require('express'),
  app = express.createServer(),
  //Application dependency modules
  settings = require('./settings'), //app settings
  routes = require('./routes'), //http routes
  rtroutes = require('./rtroutes'); //real time communication routes (io)

var io = require('socket.io').listen(app);
var appWithSettings = settings.setup(io, app);

routes.settings.setup(appWithSettings);
rtroutes.settings.setup(io, appWithSettings);

No routes are added until routes.settings.setup is called. settings (which is the global settings) is a pretty big file. That’s where all configuration is done. Settings are not added until settings.setup method is called too. Here’s a cut of the file:

//Dependency modules
var express = require('express'),
  redis = require('redis'),
//Important configuration values
var SESSION_SECRET = 'some secret thing which doesnt belong to stackoverflow!',
    insert_other_variables_here = "lalala";

//Computed general objects

var RedisStore = require('connect-redis')(express),
  redis_client = redis.createClient(REDIS_PORT, REDIS_HOST);

exports.setup = function (io, app) {
  app.configure(function () {
    app.use(express.bodyParser());
    app.use(i18next.handle);
    app.use(express.methodOverride());
    app.use(express.static(__dirname + '/public'));
    app.set('views', __dirname + '/views');
    app.set('view engine', 'swig');
    app.set('view cache', false);
    var session_store = new RedisStore({ client : redis_client});
    app.use(express.errorHandler({ dumpExceptions : true, showStack : true}));
    app.use(express.cookieParser());
    console.log("ABOUT TO ADD SESSION STORE MIDDLEWARE");
    app.use(express.session({ store : session_store, secret : SESSION_SECRET, key : "sid" }));
    console.log("AND NOW ADDED THE SESSION STORE MIDDLEWARE");
    app.use(app.router);
  });

  app.configure('development', function () {
     //some things in here, but nothing that affects app. I have commented this
     //for debugging and it changed nothing
  });

  app.configure('production', function () {
    //mostly configuration for io and some caching layers, as well as servers info
    app.use(express.errorHandler());
    app.use(express.logger({ stream : logFile }));
  });
  app.listen(WEB_PORT);
  return {
    app : app,
    //some other stuff that isn't relevant
  }
}

I have 25 routes split in 4 different files (somehow I didn’t have a need for session until now, since I was delaying some parts and everything needed was done with Mongoose). Here’s an example of how it is being done (with fake names):

routes/index.js

export.settings = require("./settings");

routes/settings.js

exports.setup = function (app_settings) {
  require("./route1")(app_settings);
  require("./route2")(app_settings);
  require("./route3")(app_settings);
};

Here’s a stripped out “route1” file (“routes/route1.js”):

module.exports = function (app_settings) {
  var app = app_settings.app;
  console.log("ABOUT TO ADD ROUTES")
  app.get("/signin", function (req, res, next) {
    console.log(req.session); //this will be undefined
  });
  app.get("/register", function (req, res, next) {
  });
  app.get('/language', function (req, res, next) {
  });
  app.post('/settings', function (req, res, next) {
  });
  console.log("ADDED ROUTES NOW!")
}
  • 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-12T12:24:14+00:00Added an answer on June 12, 2026 at 12:24 pm

    Whenever you define a route, the router gets automatically inserted into whatever the middleware stack is at the time (subsequent attempts to insert it deliberately will be ignored). Are you sure you aren’t defining any routes before you set the session handler?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm sure I have done this before in the past, but I've been in
I have done this before, but can't get it work this time :S I
I have done this before but I forgot where in my app I have
I am not an expert in Git, but I have seen this done before
It's the first time I have done anything like this but wondered what the
I know I have done this before, but it isn't working today, nor can
I have done this before but am quite new to mysqli and prepared statements
I have done this before, but still haven't mastered it. I downloaded a font
I remember I have done this before, but forgot the command. e.g. I have
I have done this before, but for some reason cannot get it to work

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.