1.1.0 • Published 8 years ago

koa-state v1.1.0

Weekly downloads
1
License
MIT
Repository
github
Last release
8 years ago

Koa state NPM version

Simple state layer middleware for Koa 2. As mentionned in official koajs:

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

1.1.0

8 years ago

1.0.1

8 years ago

1.0.0

8 years ago