Byte has built-in mechanism for creating and managing states during requests.
To attach a state to the ctx
object, call app.set
with the state name and a handler that returns the initial state value:
And use it in other middlewares or handlers (chaining is required):
The handler passed in app.set
should be asynchronous if it returns a Promise
.
Because calling app.set
is the same as registering a middleware, the following example will not work:
Validation
You can validate the state value before attaching to ctx
using app.state
.