add minimal counter
This commit is contained in:
parent
da3bd0695e
commit
a49902da00
|
@ -0,0 +1,14 @@
|
||||||
|
//const Redis = require('ioredis')
|
||||||
|
//const redis = new Redis()
|
||||||
|
|
||||||
|
var COUNTER = 0
|
||||||
|
|
||||||
|
exports.incr = (val) => {
|
||||||
|
/* default to incrementing by one */
|
||||||
|
val = val == undefined ? 1 : val
|
||||||
|
COUNTER += val
|
||||||
|
}
|
||||||
|
|
||||||
|
exports.state = () => {
|
||||||
|
return COUNTER
|
||||||
|
}
|
|
@ -1,6 +1,7 @@
|
||||||
const Koa = require('koa')
|
const Koa = require('koa')
|
||||||
const Router = require('koa-router')
|
const Router = require('koa-router')
|
||||||
const JSON = require('koa-json')
|
const JSON = require('koa-json')
|
||||||
|
const Counter = require('./counter')
|
||||||
|
|
||||||
|
|
||||||
/* DEFAULTS */
|
/* DEFAULTS */
|
||||||
|
@ -10,11 +11,12 @@ const app = new Koa()
|
||||||
const router = new Router()
|
const router = new Router()
|
||||||
|
|
||||||
router.put('/click', async ctx => {
|
router.put('/click', async ctx => {
|
||||||
ctx.body = { 'resp': 'Hello World' }
|
Counter.incr()
|
||||||
|
ctx.body = { 'counter': Counter.state() }
|
||||||
});
|
});
|
||||||
|
|
||||||
router.get('/clicks', async ctx => {
|
router.get('/clicks', async ctx => {
|
||||||
ctx.body = { 'resp': 'Hello World' }
|
ctx.body = { 'counter': Counter.state() }
|
||||||
});
|
});
|
||||||
|
|
||||||
app.use(JSON({pretty: true}))
|
app.use(JSON({pretty: true}))
|
||||||
|
|
Loading…
Reference in New Issue