November 2018
Beginner
132 pages
2h 57m
English
Registering middleware in Koa is done with the .use() method found in the application object. To register the middleware defined in the previous section:
app.use(responseTimer);
Next, we can define and register a middleware to send a response back for every route as shown here:
// ...app.use(async ctx => { ctx.body = 'Hello World';});
The complete application will look like this:
// koa-middleware.js// initialize Koaconst Koa = require('koa');const app = new Koa();// create middleware functionconst responseTimer = async (ctx, next) => ...Read now
Unlock full access