Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
confusing accessors in koa
// See https://github.com/koajs/koa/issues/215
// start: nvm run 0.11 --harmony koa-confusing-accessors
// test: curl -d '{"test":"ok"}' -H 'Content-Type: application/json' http://localhost:3000/
var koa = require('koa');
var app = module.exports = koa();
app.use(function *(next){
if (this.method === 'POST') {
this.body = '<?xml version="1.0">'
this.type = 'application/xml' // set this.type => set this.response.type
switch (this.type) { // get this.type => get this.request.type
case 'application/json':
this.body += '<type>json</type>'
break
case 'application/xml':
this.body += '<type>xml</type>'
break
default:
this.set(400)
this.body += '<error>Unsupported media type</error>'
}
} else yield next
});
if (!module.parent) app.listen(3000);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.