├── README.md ├── package.json └── index.js /README.md: -------------------------------------------------------------------------------- 1 | # express-sessions 2 | 3 | ExpressJS/Mongoose Session Storage 4 | 5 | ## Installation 6 | 7 | ``` 8 | npm install express-sessions 9 | ``` 10 | 11 | ## Usage 12 | 13 | ``` js 14 | var mongoose = require('mongoose'); 15 | 16 | mongoose.connect(); 17 | 18 | app.use(express.session({ 19 | secret: 'a4f8071f-c873-4447-8ee2', 20 | cookie: { maxAge: 2628000000 }, 21 | store: new (require('express-sessions'))({ 22 | storage: 'mongodb', 23 | instance: mongoose, // optional 24 | host: 'localhost', // optional 25 | port: 27017, // optional 26 | db: 'test', // optional 27 | collection: 'sessions', // optional 28 | expire: 86400 // optional 29 | }) 30 | })); 31 | ``` 32 | Or 33 | 34 | ``` js 35 | var redis = require('redis'); 36 | var client = redis.createClient(6379, 'localhost'); 37 | 38 | app.use(express.session({ 39 | secret: 'a4f8071f-c873-4447-8ee2', 40 | cookie: { maxAge: 2628000000 }, 41 | store: new (require('express-sessions'))({ 42 | storage: 'redis', 43 | instance: client, // optional 44 | host: 'localhost', // optional 45 | port: 6379, // optional 46 | collection: 'sessions', // optional 47 | expire: 86400 // optional 48 | }) 49 | })); 50 | ``` 51 | 52 | That's it! -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "express-sessions", 3 | "homepage": "http://github.com/konteck/express-sessions", 4 | "author": { 5 | "name": "Alex Movsisyan", 6 | "url": "http://github.com/konteck/express-sessions" 7 | }, 8 | "repository": { 9 | "type": "git", 10 | "url": "http://github.com/konteck/express-sessions" 11 | }, 12 | "description": "ExpressJS MongoDB/Redis Session Storage", 13 | "keywords": [ 14 | "sessions", 15 | "express", 16 | "mongodb", 17 | "mongoose", 18 | "redis", 19 | "cookies" 20 | ], 21 | "version": "1.0.6", 22 | "dependencies": { 23 | "express": "2.x.x", 24 | "mongoose": "4.x.x", 25 | "redis": "0.x.x" 26 | }, 27 | "main": "./lib/index.js", 28 | "readme": "# express-sessions\n\nExpressJS/Mongoose Session Storage\n\n## Installation\n\n```\nnpm install express-sessions\n```\n\n## Usage\n\n``` js\nvar mongoose = require('mongoose');\n\nmongoose.connect();\n\napp.use(express.session({\n secret: 'a4f8071f-c873-4447-8ee2',\n cookie: { maxAge: 2628000000 },\n store: new (require('express-sessions'))({\n storage: 'mongodb',\n instance: mongoose, // optional\n host: 'localhost', // optional\n port: 27017, // optional\n db: 'test', // optional\n collection: 'sessions', // optional\n expire: 86400 // optional\n })\n}));\n```\nOr\n\n``` js\nvar redis = require('redis');\nvar client = redis.createClient(6379, 'localhost');\n\napp.use(express.session({\n secret: 'a4f8071f-c873-4447-8ee2',\n cookie: { maxAge: 2628000000 },\n store: new (require('express-sessions'))({\n storage: 'redis',\n instance: client, // optional\n host: 'localhost', // optional\n port: 6379, // optional\n collection: 'sessions', // optional\n expire: 86400 // optional\n })\n}));\n```\n\nThat's it!", 29 | "_id": "express-sessions@1.0.5", 30 | "_from": "express-sessions@1.0.5" 31 | } 32 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | var express = require('express'); 2 | var mongoose = require('mongoose'); 3 | var redis = require("redis"); 4 | 5 | var MongoStore = { 6 | client: null, 7 | options: {}, 8 | get: function (sid, cb) { 9 | MongoStore.client.findOne({sid: sid}, function (err, doc) { 10 | try { 11 | if (err) return cb(err, null); 12 | 13 | if (!doc) return cb(); 14 | 15 | cb(null, doc.data); // JSON.parse(doc.data) 16 | } 17 | catch (err) { 18 | cb(err); 19 | } 20 | }); 21 | }, 22 | set: function (sid, data, cb) { 23 | try { 24 | var lastAccess = new Date(); 25 | var expires = lastAccess.setDate(lastAccess.getDate() + 1); 26 | 27 | if (typeof data.cookie != 'undefined') { 28 | expires = data.cookie._expires; 29 | } 30 | 31 | if (typeof data.lastAccess != 'undefined') { 32 | lastAccess = new Date(data.lastAccess); 33 | } 34 | 35 | MongoStore.client.findOneAndUpdate({sid: sid}, { 36 | data: JSON.parse(JSON.stringify(data)), //JSON.stringify(data) 37 | lastAccess: lastAccess, 38 | expires: expires 39 | }, { upsert: true }, cb); 40 | } 41 | catch (err) { 42 | console.log('express-sessions', err); 43 | 44 | cb && cb(err); 45 | } 46 | }, 47 | destroy: function (sid, cb) { 48 | MongoStore.client.remove({ sid: sid }, cb); 49 | }, 50 | all: function (cb) { 51 | MongoStore.client.find(function (err, doc) { 52 | if (err) { 53 | return cb && cb(err); 54 | } 55 | 56 | cb && cb(null, doc); 57 | }); 58 | }, 59 | length: function (cb) { 60 | MongoStore.client.count(function (err, count) { 61 | if (err) { 62 | return cb && cb(err); 63 | } 64 | 65 | cb && cb(null, count); 66 | }); 67 | }, 68 | clear: function (cb) { 69 | MongoStore.client.drop(function () { 70 | if (err) { 71 | return cb && cb(err); 72 | } 73 | 74 | cb && cb(); 75 | }); 76 | } 77 | } 78 | 79 | var RedisStore = { 80 | client: null, 81 | options: {}, 82 | get: function (sid, cb) { 83 | RedisStore.client.get(RedisStore.options.collection + ':' + sid, function (err, doc) { 84 | try { 85 | if (err) return cb(err, null); 86 | 87 | if (!doc) return cb(); 88 | 89 | cb(null, JSON.parse(doc)); // JSON.parse(doc.data) 90 | } 91 | catch (err) { 92 | cb(err); 93 | } 94 | }); 95 | }, 96 | set: function (sid, data, cb) { 97 | try { 98 | var lastAccess = new Date(); 99 | var expires = lastAccess.setDate(lastAccess.getDate() + 1); 100 | 101 | if (typeof data.cookie != 'undefined') { 102 | expires = data.cookie._expires; 103 | } 104 | 105 | if (typeof data.lastAccess != 'undefined') { 106 | lastAccess = new Date(data.lastAccess); 107 | } 108 | 109 | RedisStore.client.set(RedisStore.options.collection + ':' + sid, JSON.stringify(data), cb); 110 | if (RedisStore.options.expire) { 111 | RedisStore.client.expire(RedisStore.options.collection + ':' + sid, parseInt(RedisStore.options.expire)); 112 | } 113 | } 114 | catch (err) { 115 | console.log('express-sessions', err); 116 | cb && cb(err); 117 | } 118 | }, 119 | destroy: function (sid, cb) { 120 | RedisStore.client.del(RedisStore.options.collection + ':' + sid, cb); 121 | }, 122 | all: function (cb) { 123 | RedisStore.client.keys(RedisStore.options.collection + ':*', function (err, docs) { 124 | if (err) { 125 | return cb && cb(err); 126 | } 127 | 128 | cb && cb(null, docs); 129 | }); 130 | }, 131 | length: function (cb) { 132 | RedisStore.client.keys(RedisStore.options.collection + ':*', function (err, docs) { 133 | if (err) { 134 | return cb && cb(err); 135 | } 136 | 137 | cb && cb(null, docs.length); 138 | }); 139 | }, 140 | clear: function (cb) { 141 | RedisStore.client.del(RedisStore.options.collection + ':*', cb); 142 | } 143 | } 144 | 145 | var SessionStore = function (options, cb) { 146 | var options = { 147 | storage: options.storage || 'mongodb', 148 | host: options.host || 'localhost', 149 | port: options.port || (options.storage == 'redis' ? 6379 : 27017), 150 | db: options.db || 'test', 151 | collection: options.collection || 'sessions', 152 | instance: options.instance || null, 153 | expire: options.expire || 86400 154 | }; 155 | 156 | express.session.Store.call(this, options); 157 | 158 | switch (options.storage) { 159 | case 'mongodb': 160 | if (options.instance) { 161 | mongoose = options.instance; 162 | } else { 163 | mongoose.connect('mongodb://' + options.host + ':' + options.port + '/' + options.db); 164 | } 165 | 166 | var schema = new mongoose.Schema({ 167 | sid: { type: String, required: true, unique: true }, 168 | data: { type: {} }, 169 | lastAccess: { type: Date, index: { expires: parseInt(options.expire) * 1000} }, 170 | expires: { type: Date, index: true } 171 | }); 172 | 173 | MongoStore.options = options; 174 | MongoStore.client = mongoose.model(options.collection, schema); 175 | 176 | for (var i in MongoStore) { 177 | SessionStore.prototype[i] = MongoStore[i]; 178 | } 179 | break; 180 | case 'redis': 181 | if (options.instance) { 182 | RedisStore.client = options.instance; 183 | } else { 184 | RedisStore.client = redis.createClient(options.port, options.host); 185 | } 186 | 187 | RedisStore.options = options; 188 | 189 | for (var i in RedisStore) { 190 | SessionStore.prototype[i] = RedisStore[i]; 191 | } 192 | break; 193 | } 194 | 195 | 196 | if (cb) cb.call(null); 197 | } 198 | 199 | SessionStore.prototype = new express.session.Store(); 200 | 201 | module.exports = SessionStore; 202 | --------------------------------------------------------------------------------