forked from koajs/examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
49 lines (36 loc) · 732 Bytes
/
app.js
File metadata and controls
49 lines (36 loc) · 732 Bytes
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
43
44
45
46
47
48
49
var koa = require('koa');
var parse = require('co-body');
var session = require('koa-session');
var csrf = require('koa-csrf');
var route = require('koa-route');
var app = module.exports = koa();
/**
* csrf need session
*/
app.keys = ['session key', 'csrf example'];
app.use(session(app));
/**
* maybe a bodyparser
*/
app.use(function *(next) {
if (this.is('application/json')) {
this.request.body = yield parse(this);
}
yield* next;
});
/**
* csrf middleware
*/
app.use(csrf());
/**
* route
*/
app.use(route.get('/token', token));
app.use(route.post('/post', post));
function* token () {
this.body = this.csrf;
}
function* post() {
this.body = {ok: true};
}
if (!module.parent) app.listen(3000);