forked from bowin/hack-testgee
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
36 lines (31 loc) · 958 Bytes
/
index.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
'use strict';
var path = require('path'),
app = require('koa')(),
Redis = require('ioredis'),
redis = new Redis(),
router = require('koa-router')(),
koaBody = require('koa-body'),
_ = require('lodash');
module.exports = app;
router.post('/offset/:index', function*(next) {
let index = this.params.index;
redis.sadd(`offset-${index}`, this.request.body.data);
this.body = 'ok';
});
//get random element of this index
router.get('/offset/:index', function*(next) {
let index = this.params.index;
let key = `offset-${index}`;
let keyExisted = yield redis.exists(key);
if (!keyExisted) {
this.body = JSON.stringify({success: 0, error: `key ${key} not exist!`});
return;
}
let data = yield redis.spop(key);
this.body = JSON.stringify({success: 1, data});
});
app.use(koaBody());
app.use(router.routes());
app.use(router.allowedMethods());
app.use(require('koa-static')(path.join(__dirname, 'web')));
app.listen(3000);