Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Managing Sessions in Node.js? [closed]

Tags:

node.js

What is the best way to manage session variables in Node.js? Is there any library?

like image 507
Timmy Avatar asked Sep 17 '10 16:09

Timmy


People also ask

How do I keep a session alive in node JS?

Try something like: app. use( session( { secret: 'keyboard cat', cookie: { maxAge: 60000 }, rolling: true, resave: true, saveUninitialized: false } ) );

What is session handling in node JS?

Session management can be done in node. js by using the express-session module. It helps in saving the data in the key-value form. In this module, the session data is not saved in the cookie itself, just the session ID.

Where is session stored in node JS?

We can use express-session middleware to manage sessions in Nodejs. The session is stored in the express server itself. The default server-side session storage, MemoryStore , is purposely not designed for a production environment.


2 Answers

You can do that very easily using:

  • Connect: http://senchalabs.github.com/connect/

    Connects is like Rack in Ruby. It gives you an extra layer where you can "play" with authentication, sessions, cookies, among others.

Other option is to use frameworks:

  • Express.js: http://expressjs.com/

    It seems to be the most used node.js framework. Is like Sinatra for Ruby and runs on top of connect.

  • Geddy: http://geddyjs.org/

    If you want to do more complex WebApps, Geddy is the one you choose. Is like Rails for Ruby.

like image 148
donald Avatar answered Oct 15 '22 06:10

donald


Just offload it to memcache or some other caching mechanism. I wouldn't burden your servers with this sort of thing. What is the point of a super lean web server that has to remember stuff.

I would also try and develop your site as an application and not a website, or treat your website as an application, use the wonderful features of html5 such as local storage/local databases and cut down on the amount of traffic between server and client machines.

If all else fails (or site is small) then what's stopping you write your own session class. Not that difficult. Especially if its an in memory type thing. Put some timer logic to time out sessions and there you go. Damn in a dynamic language such as JavaScript, should be a cinch.

Structure should be a dictionary with key being session and value being an object containing details of last communication and capabilities (to enable access to certain features). Add a sweep function to clear out old sessions that have timed out. and bingo. A basic session service. a basic check on "is session key in list...yes/no...get details"...and I think thats it....or is there some feature that I am missing.

I personally would avoid any third party tool out there for as long as possible. Sands of time shift very quickly and you can always depend on code developed by yourself.

like image 20
WeNeedAnswers Avatar answered Oct 15 '22 08:10

WeNeedAnswers