nodejs share session code example
Example 1: nodejs share session
jx mt-keep:5 index
Example 2: nodejs share session
var app = require('express')(), server = require('http').createServer(app), Session = require('express-session');// set session middlewareapp.use(new Session({ store: new JXSessionStore(), // use custom store resave: true, saveUninitialized: false, secret: 'TEST_SECRET', cookie: { maxAge: 900000 // keep cookie }}));// get session dataapp.get('/', function (req, res) { res.send({ foo: req.session.foo || 'Not set. Go to \'/set\' to set value.', thread: process.threadId });});// set session dataapp.get('/set', function (req, res) { req.session.foo = 'bar'; res.send({ thread: process.threadId, message: 'foo value set to \'bar\' in session.' });});// start servervar port = 80;server.listen(port, function () { console.log("server (thread %s) listening on port %s", process.threadId, port);});
Example 3: nodejs share session
JXSessionStore.prototype.get = function (sessionId, callback) { var session = jxcore.store.shared.read(sessionId); var sessionParsed = session ? JSON.parse(session) : null; callback(null, sessionParsed);}JXSessionStore.prototype.set = function (sessionId, session, callback) { jxcore.store.shared.set(sessionId, JSON.stringify(session)); callback(null, session);}JXSessionStore.prototype.touch = function (sessionId, session, callback) { jxcore.store.shared.set(sessionId, session); callback(null);}JXSessionStore.prototype.destroy = function (sessionId, callback) { jxcore.store.shared.remove(sessionId); callback(null);}
Example 4: nodejs share session
var Store = require('express-session').Store;function JXSessionStore() { Store.call(this);}JXSessionStore.prototype.__proto__ = Store.prototype;
Example 5: nodejs share session
jx install [email protected] --savejx install [email protected] --save