Skip to content

Instantly share code, notes, and snippets.

@tiagopog
Created August 18, 2014 23:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tiagopog/c73918576f0ba503c1b3 to your computer and use it in GitHub Desktop.
Save tiagopog/c73918576f0ba503c1b3 to your computer and use it in GitHub Desktop.
koa_server.js
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