-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathapp.js
55 lines (43 loc) · 937 Bytes
/
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
43
44
45
46
47
48
49
50
51
52
53
54
55
const session = require('koa-generic-session');
const convert = require('koa-convert');
const bodyParser = require('koa-bodyparser');
const Koa = require('koa');
const Csrf = require('koa-csrf');
const Router = require('koa-router');
const app = module.exports = new Koa();
const csrf = new Csrf();
const router = new Router();
/**
* csrf need session
*/
app.keys = ['session key', 'csrf example'];
app
.use(convert(session()))
.use(bodyParser())
.use(csrf)
.use(router.routes())
.use(router.allowedMethods());
/**
* maybe a bodyparser
*/
app.use(async (ctx, next) => {
if (ctx.is('application/json')) {
ctx.body = ctx.request.body;
}
await next();
});
/**
* csrf middleware
*/
/**
* route
*/
router.get('/token', token);
router.post('/post', post);
async function token (ctx) {
ctx.body = ctx.csrf;
}
async function post (ctx) {
ctx.body = {ok: true};
}
if (!module.parent) app.listen(3000);