Created
August 18, 2014 23:03
-
-
Save tiagopog/c73918576f0ba503c1b3 to your computer and use it in GitHub Desktop.
koa_server.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var koa = require('koa'), | |
app = module.exports = koa(), | |
route = require('koa-route'); | |
// x-response-time | |
app.use(function *(next){ | |
var start = new Date; | |
yield next; | |
var ms = new Date - start; | |
this.set('X-Response-Time', ms + 'ms'); | |
}); | |
// logger | |
app.use(function *(next){ | |
var start = new Date; | |
yield next; | |
var ms = new Date - start; | |
console.log('%s - %s: %s ms', this.method, this.url, ms); | |
}); | |
// response | |
app.use(route.get('/:queue', show)); | |
app.use(route.post('/:queue', create)); | |
function *show(queue) { | |
this.body = 'Getting data from queue: ' + queue; | |
} | |
function *create(queue) { | |
this.body = 'Posting data into queue: ' + queue; | |
} | |
app.use(function *() { | |
this.body = 'Here goes the metadata...'; | |
}); | |
app.listen(3000); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment