diff --git a/src/counter.js b/src/counter.js new file mode 100644 index 0000000..35d9561 --- /dev/null +++ b/src/counter.js @@ -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 +} diff --git a/src/index.js b/src/index.js index 2a315c2..794e118 100644 --- a/src/index.js +++ b/src/index.js @@ -1,6 +1,7 @@ const Koa = require('koa') const Router = require('koa-router') const JSON = require('koa-json') +const Counter = require('./counter') /* DEFAULTS */ @@ -10,11 +11,12 @@ const app = new Koa() const router = new Router() router.put('/click', async ctx => { - ctx.body = { 'resp': 'Hello World' } + Counter.incr() + ctx.body = { 'counter': Counter.state() } }); router.get('/clicks', async ctx => { - ctx.body = { 'resp': 'Hello World' } + ctx.body = { 'counter': Counter.state() } }); app.use(JSON({pretty: true}))