diff --git a/src/app.js b/src/app.js index b2769f2..069a12b 100644 --- a/src/app.js +++ b/src/app.js @@ -1,12 +1,15 @@ import Koa from 'koa' import Router from 'koa-router' import JSON from 'koa-json' -import Logger from 'koa-logger' const App = (counter) => { const app = new Koa() const router = new Router() + app.use(router.routes()) + .use(router.allowedMethods()) + .use(JSON({pretty: true})) + app.on('error', (err, ctx) => { console.error('server error', err, ctx) }); @@ -24,11 +27,6 @@ const App = (counter) => { router.get('/clicks/:id', async ctx => { ctx.body = { [ctx.params.id]: await counter.state(ctx.params.id) } }); - - app.use(Logger()) - .use(JSON({pretty: true})) - .use(router.routes()) - .use(router.allowedMethods()) return app } diff --git a/src/server.js b/src/server.js index 5eb4f4f..e22f32d 100644 --- a/src/server.js +++ b/src/server.js @@ -1,4 +1,5 @@ import Redis from 'async-redis' +import Logger from 'koa-logger' import App from './app' import Counter from './counter' @@ -11,6 +12,9 @@ const LISTEN_PORT = process.env.LISTEN_PORT || 3000 const redis = Redis.createClient(REDIS_PORT, REDIS_HOST) const counter = new Counter(redis) const app = App(counter) + +app.use(Logger()) + app.listen(LISTEN_PORT) console.log(`Redis connection: ${REDIS_HOST}:${REDIS_PORT}`)