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