add minimal counter

This commit is contained in:
Jakub Sokołowski 2018-10-16 13:36:24 -04:00
parent da3bd0695e
commit a49902da00
2 changed files with 18 additions and 2 deletions

14
src/counter.js Normal file
View File

@ -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
}

View File

@ -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}))