this.respond=false for bypassing koa's response handling

closes #198
master
Jonathan Ong 2014-01-24 14:38:40 -08:00
parent ac08965988
commit 2bc3bb7327
3 changed files with 33 additions and 0 deletions

View File

@ -102,6 +102,12 @@ throw err;
error messages since you do not want to leak failure
details.
### ctx.respond
To bypass Koa's built-in response handling, you may explicitly set `this.respond = false;`. Use this if you want to write to the raw `res` object instead of letting Koa handle the response for you.
Note that using this is __not__ supported by Koa. This may break intended functionality of Koa middleware and Koa itself. Using this properly is considered a hack and is only a convenience to those wishing to use traditional `fn(req, res)` functions and middleware within Koa.
## Request aliases
The following accessors and alias [Request](request.md) equivalents:

View File

@ -184,6 +184,8 @@ function *respond(next){
yield *next;
if (false === this.respond) return;
var res = this.res;
if (res.headersSent || !res.socket.writable) return;

View File

@ -94,6 +94,31 @@ describe('app.use(fn)', function(){
})
describe('app.respond', function(){
describe('when this.respond === false', function(){
it('should bypass app.respond', function(done){
var app = koa();
app.use(function *(){
this.body = 'Hello';
this.respond = false;
var res = this.res;
setImmediate(function () {
res.setHeader('Content-Type', 'text/plain');
res.end('lol');
})
})
var server = app.listen();
request(server)
.get('/')
.expect(200)
.expect('lol')
.end(done);
})
})
describe('when HEAD is used', function(){
it('should not respond with the body', function(done){
var app = koa();