Take the 2-minute tour ×

I have the app.js with

var express = require('express')
  , routes = require('./routes')
  , MemoryStore = new express.session.MemoryStore
  , app = express();

  app.set('port', process.env.PORT || 3000);
  app.use(express.session({store: MemoryStore, secret: '123456', key: 'sid', maxAge: 60000 }));

the routes/index.js

var express = require('express')
  , tools = require('./../functions')
  , app = module.exports = express();


and the function.js

module.exports = {
  isAdmin: function (res,req,next) {
    if (req.session.level >= 16)
    else {

So my problem is when I access http://myadmin.com/user/3 I get an error of req.session undefined

How can I from the point of functions.js view get access to the session information?

share|improve this question
you have created two separate apps. i'm surprised anything is even logged. –  Jonathan Ong Mar 12 '13 at 2:11
add comment

2 Answers

app.js and index.js are creating two different apps, two different instances of express - two different servers.

Since you aren't requiring one from the other (index doesn't require app, and app doesn't require index), when you run one, only that one is running. If you run node app only app.js runs, and if you run node index only index.js runs.

You should probably look at the basic express app examples included in express's github repository:


share|improve this answer
From either app.js or index.js I can get req.session using the "node app" to initializate the server. My problem is inside the functions module. How can I get access to the req.session object. –  Gonçalo Silva Dias Mar 12 '13 at 20:19
add comment

The problem was in this line

isAdmin: function (res,req,next) {

for some reason I switched the res with the req. With

isAdmin: function (req,res,next) {

now I can get the session info.

share|improve this answer
"for some reason I switched the res with the req." - The reason is you have to have a request before you can have a response. So it makes sense that the argument ordering follows this pattern. –  Valjas Jun 13 '13 at 0:19
add comment

Your Answer


