ctx.respond
Set to `false` to bypass Koa's automatic response handling and write to `ctx.res` directly.
Syntax
ctx.respond = false Parameters
| Name | Type | Required | Description |
|---|---|---|---|
value | boolean | Yes | When set to `false`, Koa will not call `res.end()` or set any headers automatically after all middleware finishes. You take full control of the Node.js `ServerResponse`. |
Returns
void — Setter; returns nothing.
Examples
import Koa from 'koa';
import { createReadStream } from 'node:fs';
const app = new Koa();
app.use(async (ctx) => {
if (ctx.path === '/stream') {
ctx.respond = false;
ctx.res.writeHead(200, { 'Content-Type': 'text/plain' });
createReadStream('./large.txt').pipe(ctx.res);
} else {
ctx.body = 'Hello';
}
});
app.listen(3000);
Output
GET /stream → streams large.txt directly via Node.js res
Notes
Use `ctx.respond = false` only as a last resort when you need raw access
to `ctx.res` (e.g. for low-level streaming, server-sent events, or
WebSocket handshakes). Koa's response helpers (`ctx.body`, `ctx.type`,
etc.) will no longer take effect.