Koa
A comprehensive guide for developing robust, scalable, and secure Node.js applications using the Koa framework.From its SKILL.md
npx -y skills add ecelayes/roots-skills --skill koaAssembled from the repository path, not quoted from the project. Check it against their README if it does not work.
One thing to look at
- 0 stars0 stars. Stars are a popularity signal and not a quality one, but at this level it is likely that nobody has read this closely except its author, and you would be relying on your own review.
SKILL.md
1.7 KB, 397 tokens by cl100k_base, as published. Nobody here has run it
Koa.js Professional Standards
Middleware Architecture
- The Async Promise Chain: ALWAYS declare middleware as
async (ctx, next) => { ... }. - Await Next: You MUST
await next()exactly once in every middleware. - Execution Order: Code before
await next()handles the Request; code after handles the Response.
Reliability & Ops
- Graceful Shutdown: Do not kill the server instantly. Listen for system signals (
SIGTERM,SIGINT).- Pattern: Stop accepting new requests -> Close database connections -> Exit process.
- Example:
server.close(() => db.disconnect()).
- Health Checks: Always implement a
/healthendpoint for load balancers (return 200 OK if DB is connected).
Context (ctx) Mastery
- State Management: Use
ctx.stateto pass data between middleware (e.g.,ctx.state.user). NEVER pollute the global namespace. - Request Data: Access via
ctx.request.body,ctx.query, orctx.params. - Response Construction: Explicitly set
ctx.statusbefore settingctx.body.
Security Best Practices
- Error Handling: Implement a top-level
app.on('error', ...)listener and a generic try/catch middleware. Never crash the process on a request error. - Headers: Use
koa-helmetfor standard security headers. - Cookies: Always sign cookies using
ctx.cookies.set(name, val, { signed: true })and a secureapp.keys.
Routing
- Router Organization: Use
koa-router(or@koa/router). - Prefixing: Group routes by domain (e.g.,
const usersRouter = new Router({ prefix: '/users' })).
What ships with it
Read from the repository
Just SKILL.md. No reference files, no scripts.