I have an Express application with some routes, only two of them need to support sessions. I read everywhere that the middleware definition (app.use(express.session({...) applies only to the routes that comes after it, so I created this sample:
var express = require('express'); var app = express(); app.use(express.bodyParser()); app.get('/path1', function (req, res) { res.send('text response'); }); app.use(express.cookieParser()); app.use(express.session({ secret: 'secret', cookie: { maxAge: new Date(Date.now() + 2 * 60 * 1000) } })); app.get('/path2', function (req, res) { res.session.test = { "test": "test" }; res.send('text response'); }); app.listen(8088);
But this doesn't work: in /path2 res.session is undefined.
If I move the session middleware definition up - everything works, but I see that sessions are being create when calling /path1 (this is what I want to avoid)
Can someone explain how a single application can use session in only some of the routes.
Thanks!
///// UPDATE //////
After more digging - I figured it out:
Don't use: app.use(express.session({ ... }));
Instead - define the following:
var sessionMiddleware = express.session({ //session configurations }); function sessionHandler(req, res, next) { sessionMiddleware(req, res, next); }
Then apply the handler on the specific route/s that need session support:
app.get('/path_that_need_session', sessionHandler, function (req, res) { /* Do somthing with req.session */ });
This is a Node.js module available through the npm registry. Installation is done using the npm install command: Create a session middleware with the given options. Note Session data is not saved in the cookie itself, just the session ID. Session data is stored server-side.
Node.js Server Side Programming Programming Each request in app goes through multiple middleware’s in express. If one of the middleware returns the response it ends there. If any middleware wants to pass the request to next middleware, it uses next () function call at the end of its function call.
Instead, initialize the session middleware, save a reference to the resulting function then include it directly in the routes you want it on. Slightly simpler than the answer included in the update (you don't need the wrapper function). Show activity on this post.
In the first middleware, we used next () function call to pass the http request to next middleware in call stack. Middleware’s generally works as they are defined the file. Express provides a send () function to return any type of response e.g. html, text etc. we can still use the old write function as well.
Don't use app.use(express.session({ ... }))
.
Instead, initialize the session middleware, save a reference to the resulting function then include it directly in the routes you want it on.
var express = require('express'), app = express(); var session = express.session({ //session configuration }); app.use(app.router); // must come after app.use(app.router); app.get('/your/route/here', session, function(req, res){ // code for route handler goes here });
Slightly simpler than the answer included in the update (you don't need the wrapper function).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With