move logging setup to server.js
This commit is contained in:
parent
16fbf0e34b
commit
ff2907cadd
10
src/app.js
10
src/app.js
|
@ -1,12 +1,15 @@
|
||||||
import Koa from 'koa'
|
import Koa from 'koa'
|
||||||
import Router from 'koa-router'
|
import Router from 'koa-router'
|
||||||
import JSON from 'koa-json'
|
import JSON from 'koa-json'
|
||||||
import Logger from 'koa-logger'
|
|
||||||
|
|
||||||
const App = (counter) => {
|
const App = (counter) => {
|
||||||
const app = new Koa()
|
const app = new Koa()
|
||||||
const router = new Router()
|
const router = new Router()
|
||||||
|
|
||||||
|
app.use(router.routes())
|
||||||
|
.use(router.allowedMethods())
|
||||||
|
.use(JSON({pretty: true}))
|
||||||
|
|
||||||
app.on('error', (err, ctx) => {
|
app.on('error', (err, ctx) => {
|
||||||
console.error('server error', err, ctx)
|
console.error('server error', err, ctx)
|
||||||
});
|
});
|
||||||
|
@ -24,11 +27,6 @@ const App = (counter) => {
|
||||||
router.get('/clicks/:id', async ctx => {
|
router.get('/clicks/:id', async ctx => {
|
||||||
ctx.body = { [ctx.params.id]: await counter.state(ctx.params.id) }
|
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
|
return app
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
import Redis from 'async-redis'
|
import Redis from 'async-redis'
|
||||||
|
import Logger from 'koa-logger'
|
||||||
|
|
||||||
import App from './app'
|
import App from './app'
|
||||||
import Counter from './counter'
|
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 redis = Redis.createClient(REDIS_PORT, REDIS_HOST)
|
||||||
const counter = new Counter(redis)
|
const counter = new Counter(redis)
|
||||||
const app = App(counter)
|
const app = App(counter)
|
||||||
|
|
||||||
|
app.use(Logger())
|
||||||
|
|
||||||
app.listen(LISTEN_PORT)
|
app.listen(LISTEN_PORT)
|
||||||
|
|
||||||
console.log(`Redis connection: ${REDIS_HOST}:${REDIS_PORT}`)
|
console.log(`Redis connection: ${REDIS_HOST}:${REDIS_PORT}`)
|
||||||
|
|
Loading…
Reference in New Issue