From 9d32799486b6b02f0bc0e26d2ef6b7b10150793b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Soko=C5=82owski?= Date: Wed, 14 Nov 2018 16:36:28 +0100 Subject: [PATCH] add logger for development MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Jakub SokoĊ‚owski --- package.json | 1 + src/app.js | 8 +++++++- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/package.json b/package.json index ebc2444..87be7d7 100644 --- a/package.json +++ b/package.json @@ -8,6 +8,7 @@ "async-redis": "^1.1.4", "koa": "^2.5.3", "koa-json": "^2.0.2", + "koa-logger": "^3.2.0", "koa-router": "^7.4.0" }, "devDependencies": { diff --git a/src/app.js b/src/app.js index 31438e6..89d58c9 100644 --- a/src/app.js +++ b/src/app.js @@ -1,6 +1,7 @@ import Koa from 'koa' import Router from 'koa-router' import JSON from 'koa-json' +import Logger from 'koa-logger' import Counter from './counter' import Redis from 'async-redis' @@ -15,6 +16,10 @@ const router = new Router() const redis = Redis.createClient(REDIS_PORT, REDIS_HOST) const counter = new Counter(redis) +app.on('error', (err, ctx) => { + console.error('server error', err, ctx) +}); + router.put('/clicks/:id', async ctx => { counter.incr(ctx.params.id) ctx.body = { [ctx.params.id]: await counter.state(ctx.params.id) } @@ -28,7 +33,8 @@ router.get('/clicks/:id', async ctx => { ctx.body = { [ctx.params.id]: await counter.state(ctx.params.id) } }); -app.use(JSON({pretty: true})) +app.use(Logger()) + .use(JSON({pretty: true})) .use(router.routes()) .use(router.allowedMethods())