Make each koa context a duplex passthrough stream
npm install koa-duplexifyThis converts each Koa app's this into a duplex stream.
That is, you can read from it directly (from the request) and you can write to it directly (to the response).
This is really just a convenience wrapper and doesn't add much functionality.
This code is based on isaacs/duplex-passthrough.
You can read from each context directly:
``js`
app.use(function (next) {
return function *() {
this.on('data', console.log)
yield next
}
})
Or you can write to it directly:
`js`
app.use(function (next) {
return function *() {
this.write('this ')
this.write('is ')
this.end('cool.')
yield next
}
})
You can also pipe into it and from it.
For example, here's how you'd return the request body:
`js`
app.use(function (next) {
return function *() {
this.pipe(this)
yield next
}
})
`js
var koa = require('koa')
var duplexify = require('koa-duplexify')
var app = koa()
duplexify(app)
``
The MIT License (MIT)
Copyright (c) 2013 Jonathan Ong me@jongleberry.com
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.