Skip to content

Instantly share code, notes, and snippets.

@hax
Last active August 29, 2015 14:00
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 hax/11135436 to your computer and use it in GitHub Desktop.
Save hax/11135436 to your computer and use it in GitHub Desktop.
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