Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Return middleware chain promise from callback() #847

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions docs/api/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,12 @@ this.cookies.set('name', 'tobi', { signed: true });
app.context.db = db();
```

## app.usedAsMiddleware

Disables the default 404 status code and empty response.
This is very useful if you're using Koa as part of another framework, where Koa tries
to handle a request first and then falls back to the legacy framework.

## Error Handling

By default outputs all errors to stderr unless __NODE_ENV__ is "test" or `app.silent` is `true`.
Expand Down
9 changes: 7 additions & 2 deletions lib/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,10 +130,13 @@ app.callback = function(){
if (!this.listeners('error').length) this.on('error', this.onerror);

return function handleRequest(req, res){
res.statusCode = 404;
// don't set the default status code if Koa is part of a larger framework
if (!self.usedAsMiddleware) {
res.statusCode = 404;
}
var ctx = self.createContext(req, res);
onFinished(res, ctx.onerror);
fn.call(ctx).then(function handleResponse() {
return fn.call(ctx).then(function handleResponse() {
respond.call(ctx);
}).catch(ctx.onerror);
}
Expand Down Expand Up @@ -215,6 +218,8 @@ function respond() {

// status body
if (null == body) {
// don't send back the default empty response if Koa is part of a larger framework
if (this.app.usedAsMiddleware) return;
this.type = 'text';
body = this.message || String(code);
this.length = Buffer.byteLength(body);
Expand Down
38 changes: 38 additions & 0 deletions test/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -1110,3 +1110,41 @@ describe('app.response', function(){
.expect(204, done);
})
})

describe('app.usedAsMiddleware', function(){
var app = koa()
app.use(function *(next) {
if (this.url.indexOf('/koa') == 0) {
this.statusCode = 200
this.body = 'Hello from Koa!'
} else {
yield next
}
})
app.usedAsMiddleware = true
var koaHandle = app.callback()
function handle(req, res){
koaHandle(req, res).then(function(){
if (!res.finished) {
res.end('Hello from other framework!')
}
})
}
var server = http.createServer(handle).listen(0)

it('should not touch res if middleware doesn\'t', function(done){
request(server)
.get('/')
.expect(200)
.expect('Hello from other framework!')
.end(done)
})

it('should let middleware handle res', function(done){
request(server)
.get('/koa')
.expect(200)
.expect('Hello from Koa!')
.end(done)
})
})