flâneur — a map of the web's best reading

koa/docs/guide.md at master · koajs/koa

github.com · 1,664 words · saved by 1 readers

This guide covers Koa topics that are not directly API related, such as best practices for writing middleware and application structure suggestions. In these examples we use async functions as middleware - you can also use commonFunction or generatorFunction which will be a little different. Koa middleware are simple functions which return a MiddlewareFunction with signature (ctx, next). When the middleware is run, it must manually invoke next() to run the "downstream" middleware. For example if you wanted to track how long it takes for a request to propagate through Koa by adding an X-Response-Time header field the middleware would look like the following: If you're a front-end developer you can think any code before next(); as the "capture" phase, while any code after is the "bubble" phase. This crude gif illustrates how async function allow us to properly utilize stack flow to implement request and response flows: Next we'll look at the best practices for creating Koa middleware. Th

Guide This guide covers Koa topics that are not directly API related, such as best practices for writing middleware and application structure suggestions. In these examples we use async functions as middleware - you can also use commonFunction or generatorFunction which will be a little different. Table of Contents Guide Table of Contents Writing Middleware Middleware Best Practices Middleware options Named middleware Combining multiple middleware with koa-compose Response Middleware Async operations Debugging Koa HTTP2 Server-Sent Events Writing Middleware Koa middleware are simple functions

Explore this link on the map →

related reading