I'm having fully functional user signup/authentication system using express and connect middleware.
app.use(express.session({store: require('connect').session.MemoryStore( {reapInterval: 60000 * 10} ) }))
The only problem is that sessions drop every time you perform server restart.
https://github.com/remy/nodemon - and nodemon restarts node.js every time it detects a file change.
How can I have persistent sessions ?
Like your code is telling you are using MemoryStore. This is volatile and gets cleared on restart. I would advise you to use connect_redis to persist your session. Redis is an extremely fast store.
make
./redis-server
npm install connect-redis
var connect = require('connect') , RedisStore = require('connect-redis'); connect.createServer( connect.cookieParser(), // 5 minutes connect.session({ store: new RedisStore }) );
This is just to get you started quickly. You should read the documentation and configure redis if you want to get most out of redis.
I was trying to get Redis on track using express.js, Google sent me here. The express implementation changed:
var express = require('express'),
RedisStore = require('connect-redis')(express);
Another important thing is the order of express configurations.
app.configure(function(){
app.enable('strict routing'); // removes trailing slash
app.set('views', __dirname + '/views');
app.set('view engine', 'jqtpl');
app.register('.html', require('jqtpl').express);
app.use(express.favicon());
app.use(express.methodOverride());
app.use(express.compiler({src: __dirname + '/public', enable: ['sass']}));
app.use(express.static(__dirname + '/public'));
app.use(app.router);
app.use(express.bodyParser());
app.use(express.cookieParser());
app.use(express.session({secret: _.config.secret, store: new RedisStore}));
});
cookieParser
& session configurations need to be at the end of the configurations, and cookieParser
must be placed right before express.session
.
Hope that helps, I ran in both of these problems.
I agree with everybody about redis, but I think that different technologies is a problem in terms of software maintenance. If you are using mongodb for example there is connect-mongo ( https://npmjs.org/package/connect-mongo ), if you are using mysql there is connect-mysql ( https://npmjs.org/package/connect-mysql ), connect-couchdb for couchdb ( https://npmjs.org/package/connect-couchdb ) and so on.
also, if you're using express, you need to provide a secret when telling the app to use the redis middleware.
so, follow Alfred's recipe above, but do the following...
var express = require( 'express' );
var RedisStore = require('connect-redis');
app.use( express.cookieParser() );
app.use( express.session( { secret: "keyboard cat", store: new RedisStore }));
When node dies I would imagine the memory store you're using dies.
Persist the sessions to disk?
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.