I was messing around with Express JS and was the using express-session. It didn't work if I didn't put the app.use(session())
middleware first
app.use(session({
name: 'server-session-cookie-id',
secret: 'my express secret',
saveUninitialized: true,
resave: true
}));
app.use('/', routes);
app.set("view engine", "pug");
app.set("views", path.join(__dirname + "/views"));
app.use(flash());
If the app.use(session())
written first it would say that the req.session.someVariable
was undefined.
Can someone explain how Express calls the app.use middleware?
All Express route handlers (whether app.use()
, app.get()
, app.post()
or anything else) are checked for a route match in the order they are registered. Further, once a route sends a request and does not call next()
, then no further routes are checked for a match to the current request. All processing for that request will be done.
So, you HAVE to put any middleware BEFORE any requests that want to use the results of that middleware.
Some people think that all app.use()
handlers might be checked for a match and potentially executed (if they match the request) before any app.get()
or app.post()
handlers. That is not the case. There's a single handler order in the order they are registered, regardless of which type of handler it is.
It didn't work if I didn't put the
app.use(session())
middleware first.
If the session middleware isn't before a request handler that wants to use the session, then the session variables won't yet be initialized when that request handler runs and req.session
itself will be undefined
.
You have to place the session middleware before any request handler that wants to use the session variables.