Full stack items | small bookcase | -Koa2 middleware server development mechanism onion model look

KOA2

What is?

Koa is a new webframework, the Express build the same people behind the scenes, to become webapplication and APIdevelopment in the field of a smaller, more expressive, more robust foundation. By using the asyncfunction, Koato help you discard callback function, and effectively enhanced error handling. KoaAnd not tied to any 中间件, but it provides a graceful way to help you quickly and happily write server-side applications.

Why produce?

The author of several of these frameworks are not familiar with, there is not fraught. Here are some you can look at the introduction of big brother.

KoaIs Expressto create the same people, so why do not they will be Expressan upgraded version of it but the new development of a new project? See the official description: Koa and Express

Middleware

Middleware (English: Middleware), also translated middleware, interposer, is software that provides the connection between the system software and application software, to facilitate communication between the various software components, in particular for applications centralized logic system software, in the framework of modern information technology applications such as Webservices, and other service-oriented architecture used widely. Middleware - Wikipedia

Middleware is just a service, without which the system can also be present. Such as: a desktop computer, it is assembled from a number of components together. Mouse, keyboard, etc. just to make the system more perfect. Even without a mouse or keyboard, the desktop can also use other hardware and software to operate a computer. Or view AOP Aspect Oriented Programming in metaphor.

Very useful Koamiddleware collection

KoaIs a middleware framework, two different methods may be employed to implement the middleware:

  • async function
  • common function

The following is an example of using two different methods to achieve a log middleware:

Middleware typically takes two arguments (ctx, next), ctxis a request context (context),
nexta middleware function call performed downstream. After the completion of the code executed by the thenmethod returns a Promise. About PromiseIntroduction You can view this article: Promise Promise principle explain && achieve a target (follow Promise / A + specification)

async functions (node v7.6+)

app.use(async (ctx, next) => {
  const start = Date.now();
  await next();
  const ms = Date.now() - start;
  console.log(`${ctx.method} ${ctx.url} - ${ms}ms`);
});

Common function

app.use((ctx, next) => {
  const start = Date.now();
  return next().then(() => {
    const ms = Date.now() - start;
    console.log(`${ctx.method} ${ctx.url} - ${ms}ms`);
  });
});

KoaMiddleware mechanism is used 洋葱模型, and what is the onion model it?

Onion model

By next()the effect of the plurality of intermediate series performed. All middleware will be executed on both sides, like an onion, the onion from one side into the other side will go out.

1 illustrates a model onion rings source
1 onion model icon
is the process response to a request from the middleware will all go twice. Not clear to see Scheme 2 can be seen.
2 illustrates a model of onion rings source
2 illustrates a model of onion
by way of illustration may not be very apparent Koaexecution model onion, a look at the following simple example:

const Koa = require('koa');

const app = new Koa();

// 中间件1
app.use(async (ctx, next)=>{
    console.log(1)
    next()
    console.log(11)
});
// 中间件2
app.use(async (ctx, next) => {
    console.log(2)
    next()
    console.log(22)
})

// 中间件3
app.use(async (ctx, next) => {
    console.log(3)
    next()
    console.log(33)
})

app.listen('3000');
console.log(`http://localhost:3000`);

Enter the browser http://localhost:3000output after the carriage return:

http://localhost:3000
1
2 
3 
33
22
11

And onions can be seen that the process performed by the model are consistent results.

reference


Advice please add micro letter: Light to tease.
Here Insert Picture Description

Guess you like

Origin www.cnblogs.com/gdragon/p/11875839.html