Passing Information Through Koa Middleware Via "this.state"
Koa has a context object created during every request/response cycle. The context is referenced via this in the middleware. Much information are captured in the context, such as req and res. In order to avoid polluting the context and colliding with other middleware, the information from your application should be encapsulated into this.state property, a namespace for passing information through middleware during request/response cycle:
|
|
The this.state has been initialized into an empty object:
|
|
Therefore, we can start using it right away without doing our own initialization:
|
|