how to use sessions in express, couchDB, and node.js

19,554

Solution 1

If you're doing app.use(app.router) before the lines that set up the cookie and session handling, try moving it to after them. That fixed the same problem for me.

Solution 2

I was trying to solve the exact same problem for he last few hour.

Try initializing your server like that:

var app = express.createServer(
express.cookieParser(),
express.session({ secret: "crazysecretstuff"})
  );

That should work.

Share:
19,554

Related videos on Youtube

Ernesto11
Author by

Ernesto11

Updated on July 15, 2020

Comments

  • Ernesto11
    Ernesto11 almost 4 years

    so I basically want to use sessions to store the users name and check to see if the user logged in or not. If not, the page will redirect to the login page.

    I am using Node.js,express,and couchDB.

    Here is how i set up my session so far

    var MemoryStore = require('connect').session.MemoryStore;
    app.use(express.cookieParser());
    app.use(express.session({ 
        secret: "keyboard cat", 
        store: new MemoryStore({ 
            reapInterval: 60000 * 10
        })
    }));
    

    To store something in the session, i use the following code right?

    req.session = {user:name);
    

    So the session variable seems to work on my login page. I successfully store the user's name into the session However, when I try to access the session variable on another page, it gives me the error

    Cannot read property 'user' of undefined
    

    all i'm doing is:

    if (req.session.user){
    

    Why is this error happening? are sessions not global to the whole app? Or am I missing something entirely here.

    Thanks in advance!

    • twiz
      twiz about 11 years
      The req.session = {user:name); part of your code is no longer correct. I assume this worked in an older version of express, but now to avoid an error, you will need to use req.session.user=name;. If you don't, methods on req.session will be overwritten.
  • Ernesto11
    Ernesto11 about 13 years
    I tried that and it's still not working. Why would I have to initialize my server like that? it says in the express documentation to use
  • Ernesto11
    Ernesto11 about 13 years
    app.use(express.cookieParser()); app.use(express.session({ secret: "keyboard cat" }));
  • marc_d
    marc_d about 13 years
    Both are valid ways to initialize middleware with express.js. The only difference for me is that sessions only work if I initialize them in app.createServer. Might be a bug.
  • Ernesto11
    Ernesto11 about 13 years
    Ok yeah. Everything works now. Idk why it wasn't earlier. Thanks a lot guys!
  • RobKohr
    RobKohr over 12 years
    Just joining the others who have stumbled upon your answer when need of help... thanks!
  • nak
    nak over 12 years
    @Ernesto11 you should give this a green check :) It worked for me as well
  • CpILL
    CpILL about 12 years
    no work for me. Only adding it to the express.createServer() call seems to make it happen :-/
  • Yuri Subach
    Yuri Subach almost 12 years
    Works for me, thank you. The main idea IMHO is to 'use()' cookieParser and session before all other 'uses'. My code: var app = express.createServer(); app.use(express.cookieParser()); app.use(express.session({ secret: 'secret key' })); app.use(express.bodyParser());
  • nax
    nax almost 12 years
    Just to clarify, I had the same problem but this doesn't fix that at all. But I discovered you have to put the cookieParser, session and router at the end of the configuration in that order. hope someone more helps!
  • Farzher
    Farzher over 11 years
    You need app.use(express.cookieParser()); before app.use(express.session({ secret: '}{91/,48+F{tf2'}));`

Related