Koa

From TerminalSkills/skills · View interactive version

Koa is an AI agent skill from the GitHub repository TerminalSkills/skills. Koa is a minimalist Node.js web framework from the team behind Express, built around async/await middleware and a single context object. Use when a user asks to build a REST API or web service with Koa, add routing, body parsing, CORS or error handling to a Koa app, write custom middleware, or migra

How to use this skill

Skill file

Koa — Next-Generation Node.js Web Framework

Overview

Koa (current major version 3.x) is a small framework: it ships the application object, the ctx context (request plus response helpers) and the middleware pipeline, and nothing else. Routing, body parsing, CORS and logging are separate packages you add. Middleware are async functions that call await next(); code before the call runs on the way down, code after it runs on the way back up (the "onion" model).

Koa 3 requires Node.js 18 or newer, no longer accepts generator-function middleware (koa-convert is gone), and builds ctx.query with URLSearchParams. @koa/router 15 requires Node.js 20 or newer.

Instructions

Install

npm install koa @koa/router @koa/bodyparser @koa/cors koa-logger
npm install -D @types/koa @types/koa-logger @types/koa__cors   # TypeScript only

@koa/router ships its own types, so do not install @types/koa__router. Koa itself has no bundled types: add @types/koa. koa-compose comes in as a dependency of Koa; install it explicitly if you import it directly.

Application skeleton

import Koa from "koa";
import Router from "@koa/router";
import { bodyParser } from "@koa/bodyparser";   // named export, not default
import cors from "@koa/cors";
import logger from "koa-logger";

const app = new Koa();
const router = new Router({ prefix: "/api" });

// 1. Error handler goes first so it wraps everything below it
app.use(async (ctx, next) => {
  try {
    await next();
  } catch (err) {
    ctx.status = err.status || 500;
    ctx.body = { error: err.expose ? err.message : "Internal Server Error" };
    ctx.app.emit("error", err, ctx);          // still reaches app.on("error")
  }
});

app.use(logger());
app.use(cors());
app.use(bodyParser());                         // JSON and urlencoded by default

router.post("/orders", async (ctx) => {
  const { customerId, items } = ctx.request.body;
  if (!customerId || !Array.isArray(items)) {
    ctx.throw(400, "customerId and items are required");
  }
  ctx.status = 201;
  ctx.body = { id: "ord_1042", customerId, itemCount: items.length };
});

router.get("/orders/:id", async (ctx) => {
  ctx.assert(ctx.params.id.startsWith("ord_"), 404, "Order not found");
  ctx.body = { id: ctx.params.id };
});

app.use(router.routes()).use(router.allowedMethods());   // 405/OPTIONS handling

app.on("error", (err) => console.error("unhandled", err));
app.listen(3000, () => console.log("listening on :3000"));

Context essentials

Routing with @koa/router 15

Middleware composition

import compose from "koa-compose";

const requireAdmin = compose([requireAuth, requireRole("admin"), auditLog]);
router.delete("/users/:id", requireAdmin, deleteUser);

async function requireAuth(ctx, next) {
  const token = ctx.get("Authorization").replace(/^Bearer /, "");
  if (!token) ctx.throw(401, "Missing bearer token");
  ctx.state.user = await verifyToken(token);   // your own verification
  await next();
}

Multipart uploads and other body types

@koa/bodyparser does not parse multipart. Use @koa/multer (or busboy) for file uploads. Other types are opt-in: bodyParser({ enableTypes: ["json", "form", "text"], jsonLimit: "2mb" }). Defaults: jsonLimit and textLimit 1mb, formLimit 56kb.

Examples

Example 1: Add a health endpoint and response-time header

User request: "Add a /health route to my Koa app and a header with how long each request took."

app.use(async (ctx, next) => {
  const start = process.hrtime.bigint();
  await next();                                  // everything downstream runs here
  const ms = Number(process.hrtime.bigint() - start) / 1e6;
  ctx.set("X-Response-Time", `${ms.toFixed(1)}ms`);
});

router.get("/health", (ctx) => {
  ctx.body = { status: "ok", uptimeSeconds: Math.round(process.uptime()) };
});

Check it: curl -i http://localhost:3000/api/health returns 200, a JSON body such as {"status":"ok","uptimeSeconds":12} and an X-Response-Time: 0.4ms header.

Example 2: Migrate an Express handler

User request: "Convert this Express route to Koa: app.post('/login', (req, res, next) => { ... res.status(401).json({error:'bad credentials'}) })."

router.post("/login", async (ctx) => {
  const { email, password } = ctx.request.body;
  const user = await users.findByEmail(email);
  if (!user || !(await verifyPassword(user, password))) {
    ctx.throw(401, "bad credentials");           // replaces res.status(401).json(...) and next(err)
  }
  ctx.body = { token: await issueToken(user) };
});

Result: the request returns 401 {"error":"bad credentials"} through the error-handling middleware shown above; there is no next(err) call, errors propagate as rejected promises.

Guidelines

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.