https://github.com/rickharrison/koa-flash
Flash messages for your koa application.
https://github.com/rickharrison/koa-flash
Last synced: about 1 year ago
JSON representation
Flash messages for your koa application.
- Host: GitHub
- URL: https://github.com/rickharrison/koa-flash
- Owner: rickharrison
- License: mit
- Created: 2014-03-20T17:22:05.000Z (over 12 years ago)
- Default Branch: master
- Last Pushed: 2021-11-02T10:54:13.000Z (over 4 years ago)
- Last Synced: 2025-04-15T02:18:39.598Z (over 1 year ago)
- Language: JavaScript
- Size: 234 KB
- Stars: 39
- Watchers: 1
- Forks: 5
- Open Issues: 5
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
#koa-flash
Flash messages for your [koa](https://github.com/koajs/koa) application.
[](https://travis-ci.org/rickharrison/koa-flash)
## Installation
```js
$ npm install koa-flash
```
koa-flash also depends on [koa-session](https://github.com/koajs/session). You must add koa-session as a middleware prior to adding koa-flash as seen in the example:
## Example
```js
var koa = require('koa')
, session = require('koa-session')
, flash = require('koa-flash');
var app = koa();
app.keys = ['foo'];
app.use(session());
app.use(flash());
app.use(function *() {
if (this.method === 'POST') {
this.flash = { error: 'This is a flash error message.' };
} else if (this.method === 'GET') {
this.body = this.flash.error || 'No flash data.';
}
});
app.listen(3000);
```
## Semantics
Flash data when set will be saved to the user's session for exactly one more request. You can save any javascript object into `this.flash` (Object, Number, String, etc.). A common use case is to save an error message from a `POST` request when redirecting to a `GET` request to display the form again.
## Options
Flash data is saved into `this.session['koa-flash']` by default. You can change this by passing in a `key` option.
```js
app.use(flash({ key: 'foo' }));
```
Also, you can set `defaultValue` instead of `{}`.
```js
app.use(flash({ defaultValue: 'bar' }));
```
## License
MIT