1. The Context object

Koa provides a Context object that represents the Context of a conversation (both HTTP request and HTTP reply). By manipulating this object, you can control what is returned to the user.

The context.response. body property is the content sent to the user

const Koa = require('koa');
const app = new Koa();

const main = ctx => {
  ctx.response.body = 'Hello World';
};

app.use(main);
app.listen(3000);
Copy the code

In the code above, the main function is used to set ctx.response.body. The main function is then loaded using the app.use method.

As you might have guessed, ctx. Response stands for HTTP Response. Likewise, ctx.request stands for HTTP Request.

2. The routing

Native routes are not very convenient to use, we can use the encapsulated KOA-Route module

const route = require('koa-route');

const about = ctx => {
  ctx.response.type = 'html';
  ctx.response.body = '<a href="/">Index Page</a>';
};

const main = ctx => {
  ctx.response.body = 'Hello World';
};

app.use(route.get('/', main));
app.use(route.get('/about', about));
Copy the code

Static resources

If the site provides static resources (images, fonts, stylesheets, scripts……) Writing routes for each of them is cumbersome and unnecessary. The KOA-Static module encapsulates this part of the request.

const path = require('path');
const serve = require('koa-static');

const main = serve(path.join(__dirname));
app.use(main);
Copy the code

4. The middleware

const logger = (ctx, next) => {
	  console.log(`${Date.now()} ${ctx.request.method} ${ctx.request.url}`);
  next();
}
app.use(logger);
Copy the code

A logger function like the one above is called “middleware” because it sits between an HTTP Request and an HTTP Response and performs some intermediate function. App.use () is used to load middleware.

Basically, all of Koa’s functionality is implemented through middleware, as was Main in the previous example. Each middleware accepts two parameters by default, the first being the Context object and the second being the next function. Simply call the next function to pass execution to the next middleware

5. Comparison of Express and KOA middleware

Express middleware executes one after another. Koa middleware completes in a circle, from outer layer to inner layer and back to outer layer.

  • Koa2’s middleware is implemented with async await and the middleware execution order is the “onion ring” model. When one middleware calls next(), it transfers control to the next middleware, and when the next middleware stops executing next(), it switches back down the road and transfers control to the previous middleware in turn.
  • Unlike KOA2 middleware, Express middleware executes sequentially, usually writing the response response in the last middleware. App. use is used to register the middleware with HTTP request. According to path and method, which middleware will be triggered to implement the next mechanism, that is, the last middleware will trigger the next middleware through next