app.listen()

Creates and starts an HTTP server bound to the given port (shorthand for `http.createServer(app.callback()).listen(...)`).

Since Koa 2 Spec ↗

Syntax

app.listen([port[, hostname[, backlog]]][, callback])

Parameters

NameTypeRequiredDescription
port number No TCP port to listen on. Defaults to an OS-assigned ephemeral port when omitted or 0.
hostname string No Hostname or IP address to bind. Defaults to `'::'` (all interfaces).
callback function No Called once the server is listening.

Returns

http.Server — The underlying Node.js HTTP server instance.

Examples

import Koa from 'koa';

const app = new Koa();

app.use(async (ctx) => {
  ctx.body = { status: 'ok' };
});

const server = app.listen(3000, () => {
  console.log('Server listening on port 3000');
});
Output
Server listening on port 3000
// HTTPS server using app.callback()
import https from 'node:https';
import fs from 'node:fs';

const server = https.createServer(
  { key: fs.readFileSync('key.pem'), cert: fs.readFileSync('cert.pem') },
  app.callback()
).listen(443);
Output
HTTPS server running on port 443

Notes

For HTTPS or HTTP/2, use `app.callback()` directly and pass it to the relevant `createServer` call. The returned `http.Server` instance can be used to set timeouts or integrate with WebSocket libraries.

See also