Sovos
Sovos

Reputation: 3390

Node.js: is app.locals a global obj?

Is app.locals a global variable? IE: will it persist between users or is it safe for storing data like userId etc (instead of passing every time through the req.session obj)?

Upvotes: 1

Views: 2050

Answers (1)

raina77ow
raina77ow

Reputation: 106385

In Express...

  • app is usually the name of the variable that stores an application - object returned by express()
  • req is usually the name of the param of the function that handles a specific type of request

The key difference is, obviously, the lifetime of the corresponding objects: the one stored in req lives as long as particular request is served (after that the handler function just finishes, taking all the local params - and arguments - with it), the one stored in app lives as long as application is working.

The bottom line - the data stored in app.locals is persistent between requests.

Upvotes: 4

Related Questions