KOA2
What is?
Koa is a new
web
framework, the Express build the same people behind the scenes, to becomeweb
application andAPI
development in the field of a smaller, more expressive, more robust foundation. By using theasync
function,Koa
to help you discard callback function, and effectively enhanced error handling.Koa
And 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.
Koa
Is Express
to create the same people, so why do not they will be Express
an 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 asWeb
services, 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.
Koa
Is 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), ctx
is a request context(context)
,
next
a middleware function call performed downstream. After the completion of the code executed by thethen
method returns aPromise
. AboutPromise
Introduction 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`);
});
});
Koa
Middleware 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
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
by way of illustration may not be very apparent Koa
execution 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:3000
output 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.