01
Getting Started
Hello Server
Koa uses async/await instead of callbacks.
koa
import Koa from "koa";
const app = new Koa();
app.use(async ctx => {
ctx.body = "Hello World";
});
app.listen(3000);Middleware
Middleware is a cascade — await next() to pass control.
koa
app.use(async (ctx, next) => {
const start = Date.now();
await next();
const ms = Date.now() - start;
console.log(`${ms}ms`);
});Router
koa-router for routing.
koa
import Router from "koa-router";
const router = new Router();
router.get("/users/:id", async ctx => {
ctx.body = `User ${ctx.params.id}`;
});
app.use(router.routes());Was this helpful?