move logging setup to server.js

This commit is contained in:
Jakub Sokołowski 2018-11-15 13:18:28 +01:00
parent 16fbf0e34b
commit ff2907cadd
No known key found for this signature in database
GPG Key ID: 4EF064D0E6D63020
2 changed files with 8 additions and 6 deletions

View File

@ -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)
});
@ -25,11 +28,6 @@ const App = (counter) => {
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
}

View File

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