Timo Timo - 24 days ago 8
CoffeeScript Question

Making Session Data Accessible From Everywhere (In An Express.js Project)

I am using Express-Middleware in an outsourced file.

I set some session data in that outsourced file:

req.session.userId


Now I want to access this variable in my root file (app.js), but there it is NULL.

How can I make this session data accessible from everywhere?

Answer

Okay. You simply declare the variable as global:

./app.js

global.session = { }

Now you can reach this object from everywhere:

global.session.userId = 1

As seen here:

Global session variable in express.js route?

Comments