01
Primeros pasos
Servidor básico
Koa usa async/await en lugar de callbacks.
koa
import Koa from "koa";
const app = new Koa();
app.use(async ctx => {
ctx.body = "Hello World";
});
app.listen(3000);Middleware
El middleware es una cascada — usa await next() para pasar el 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 para el enrutamiento.
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?