-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathapp.js
42 lines (32 loc) · 1.06 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
/**
* A very simple flash example.
* Only uses JSON for simplicity.
*/
const rawBody = require('raw-body');
const session = require('koa-session');
const Koa = require('koa');
const app = module.exports = new Koa();
// required for signed cookie sessions
app.keys = ['key1', 'key2'];
app.use(session(app));
app.use(async (ctx, next) => {
if (ctx.method !== 'GET' || ctx.path !== '/messages') return await next();
// get any messages saved in the session
const messages = ctx.session.messages || [];
ctx.body = messages;
// delete the messages as they've been deliverd
delete ctx.session.messages;
});
app.use(async (ctx, next) => {
if (ctx.method !== 'POST' || ctx.path !== '/messages') return await next();
// the request string is the flash message
const message = await rawBody(ctx.req, {
encoding: 'utf8'
});
// push the message to the session
ctx.session.messages = ctx.session.messages || [];
ctx.session.messages.push(message);
// tell the client everything went okay
ctx.status = 204;
});
if (!module.parent) app.listen(3000);