app.callback()

Returns a Node.js `http.IncomingMessage` handler suitable for `http.createServer()` or test frameworks.

Since Koa 2 Spec ↗

Syntax

app.callback()

Returns

function — A `(req, res) => void` request handler compatible with Node.js `http.createServer`.

Examples

import Koa from 'koa';
import http from 'node:http';

const app = new Koa();

app.use(async (ctx) => {
  ctx.body = 'Hello from callback';
});

const server = http.createServer(app.callback());
server.listen(3000);
Output
$ curl localhost:3000
Hello from callback
// Use with supertest for unit testing
import request from 'supertest';

const res = await request(app.callback()).get('/');
console.log(res.status); // 200
Output
200

Notes

`app.listen(port)` is a thin wrapper around `http.createServer(app.callback()).listen(port)`. Use `app.callback()` directly when you need access to the raw server (e.g. WebSocket upgrades, HTTPS, HTTP/2) or when writing tests without binding a real port.

See also