https://github.com/tilap/koa-state
Simple state layer middleware for Koa 2
https://github.com/tilap/koa-state
Last synced: over 1 year ago
JSON representation
Simple state layer middleware for Koa 2
- Host: GitHub
- URL: https://github.com/tilap/koa-state
- Owner: tilap
- License: mit
- Created: 2016-04-24T17:34:19.000Z (about 10 years ago)
- Default Branch: master
- Last Pushed: 2016-05-01T08:12:26.000Z (about 10 years ago)
- Last Synced: 2025-03-07T18:50:55.550Z (over 1 year ago)
- Language: JavaScript
- Homepage:
- Size: 5.86 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
Koa state [![NPM version][npm-image]][npm-url]
============================
Simple state layer middleware **for Koa 2**. As mentionned in official [koajs](http://koajs.com/):
> ctx.state
The recommended namespace for passing information through middleware and to your frontend views.
## Install
```
npm install --save koa-state
```
## Usage
```
const Koa = require('Koa');
const koaState = require('koa-state');
const app = new Koa();
app.use(koaState());
app.use(async (ctx, next) => {
if (!ctx.hasState('my-key-state')) {
ctx.setState('my-key-state', 'anything-as-value');
}
await next();
});
app.use(async (ctx, next) => {
console.log(ctx.getState('my-key-state'));
await next();
});
app.listen(3000);
```
## Options
You can customize the context key with ```app.use(koaState('contextKey'));```
## License
MIT
[npm-image]: https://img.shields.io/npm/v/koa-state.svg?style=flat
[npm-url]: https://npmjs.org/package/koa-state