-
Notifications
You must be signed in to change notification settings - Fork 4
/
server.js
50 lines (43 loc) · 1.03 KB
/
server.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
"use strict"
var koa = require('koa');
var app = koa();
var port = process.env[2] || 8889;
var send = require('koa-send');
var serve = require('koa-static');
var parse = require('co-body');
app.use(serve(__dirname + '/public'));
app.use(function*(next) {
if (this.path === '/') {
yield send(this, 'index.html', {
root: __dirname + '/public'
});
} else {
yield next;
}
});
app.use(function*(next) {
if (this.path === '/api/login' &&
this.request.method === 'POST'
) {
const body = yield parse(this, {
textTypes: ['text']
})
yield delay(2000)
this.response.type = 'json'
if (!!body.username && !!body.password) {
const result = {
username: body.username,
token: 'asdadasflasfaasda'
}
this.body = result
}
} else {
yield next;
}
});
function* delay(timeout) {
yield function(done) {
setTimeout(done, timeout)
}
}
app.listen(port)