Node.js: app.locals global object?

Is app.locals a global variable? IE: will it persist between users or is it safe to store data like userId etc. (Instead of going through req.session obj every time)?

+1
express
source share
1 answer

In express ...

  • app usually the name of the variable in which the application object returned by express() is stored
  • req usually the name of a function parameter that processes a particular type of request

The key difference is, obviously, the lifetime of the corresponding objects: the one stored in req is saved until a specific request is serviced (after that the handler function ends, taking all local parameters - and arguments - with it), the one that is stored in the app , works as long as the application is running.

On the bottom line, the data stored in app.locals is saved between requests.

+3
source share

All Articles