You'll need something like connect to track session state for you as that is the least-trivial of these scopes. Once you have that you can do things as follows.
- Application scoped (= same as the singleton pattern)
- set a property on your HTTP server instance:
myServer.totalRequestsServed = 0
- or you can use a process-wide global:
global.totalRequestsServed = 0
- set a property on your HTTP server instance:
- Session scoped (state is kept for a user's session lifecycle, e.g. the logged in user)
- use the connect session middleware for this:
req.session.user = myUser
- use the connect session middleware for this:
- Conversation scoped (state is kept for an identified conversation, e.g. a shopping cart)
- I'm not totally clear on what you mean by "conversation scope" but I would stick this on the session user or in a shared hash object.
session.user.shoppingCart = {}
- I'm not totally clear on what you mean by "conversation scope" but I would stick this on the session user or in a shared hash object.
- Flash scoped (state is kept between two requests)
- Use either the session or the request for this. There are existing modules in npm to give you helpers like
req.flash("Welcome!")
. See connect-flash for example.
- Use either the session or the request for this. There are existing modules in npm to give you helpers like