
"Next generation web framework for node.js" - this is written in the documentation for version 1.0. It sounds good, I’ll add to this that 2 years ago after introducing koa on one of our projects, our programmers got the term “pseudo-synchronous code” (This is when the code looks like synchronous but is actually executed asynchronously).
What kind of
nonsense How it works, I will tell under the cut.
What is KoaJS
Not to repeat:
Using the features of the generators and the
yield directive, we can asynchronously execute
Promise or a specially constructed thinkify function and, after executing the promise / function, return to the point where
yield was called to return the result and continue executing the code.
An example of such a code:
Why do I need KoaJS?
The idea of Koa is ideally placed on the paradigm of microservices, which I have long ago implemented in our company. The core of the framework is minimalistic, the middleware code is crammed and understood very easily, which is very important for team development. Actively used by the framework capabilities ES2015, minimal trauma to the psyche of the programmer, who had previously written in PHP (these guys do not like Kolbeki :)).
')
Wonderful, what will surprise KoaJS 2.0?
What was the basis of KoaJS, namely the co library, built on generators is now removed from the basic Koa 2 implementation.
Let's go
Hello world!
const Koa = require('koa'); const app = new Koa();
As you can see, there are no generators here, but then how can I write a “pseudo-synchronous code” praised by me. In the simplest case, you can get by with the native code:
app.use((ctx, next) => { const start = new Date(); return next().then(() => { const ms = new Date() - start; console.log(`${ctx.method} ${ctx.url} - ${ms}ms`); }); });
next is a promise, ctx is a request context.
In this form, many things cannot be implemented without callbacks, so the authors propose to use the new
async / await syncasis, which has not yet become a standard and is not natively supported by NodeJS but has long been implemented in the
Babel transpiler. It looks like this
app.use(async (ctx, next) => { const start = new Date(); await next(); const ms = new Date() - start; console.log(`${ctx.method} ${ctx.url} - ${ms}ms`); });
There is also a variant with an additional connection of the
co library and generators:
app.use(co.wrap(function *(ctx, next) { const start = new Date(); yield next(); const ms = new Date() - start; console.log(`${ctx.method} ${ctx.url} - ${ms}ms`); }));
Compatible with Koa 1.x
When I talked about the "sawed" generators, it was not entirely accurate. For compatibility, if you execute middleware in the style of koa 1.0, koa 2.0 connects it and executes it at the same time it will be “Koa deprecated Support for generators will be removed in v3 ...” in other words, everything will work before version 3.x.
Here is an example:
You can also convert existing middleware to 2.0 format by yourself using the koa-convert module
const convert = require('koa-convert'); app.use(convert(function *(next) { const start = new Date(); yield next; const ms = new Date() - start; console.log(`${this.method} ${this.url} - ${ms}ms`); }));
In this case, there are no warnings to the console, so I recommend using this way of connecting legacy middleware.
Why go to 2.0
Of course, I cannot say 100%, but six months of stable operation of one of the typical services give me the confidence that 2.0 is fairly stable.
I want to have the right to choose how to implement my middleware. Koa 2.0 gives me three ways: native, generators and async / await
Koa 2.0 already supports many popular middlewares, and if they don’t, they work through koa-convert
If you are now looking at new frameworks, try a little code on Koa - I am sure you will not regret taking the time to do so.
What to see