2019 NodeJS framework Koa Express and the selection of comparison

Tribute to the original author

  +

Koa and Express are NodeJS mainstream application development framework.

Nodejs Express is a complete application framework. Koa by Express team development, but it has a different focus. Koa committed to the core middleware functions. Middleware is nodejs access request object (req) and response objects (RES) routine. The routine is called before the route processing routine, and therefore they are in the client and the routing logic generates a response "intermediate." Application middleware nodejs routine "link" may be the custom request / response pipeline. Conduit may operate in accordance with the request and the response, including the head and body. Express and Koa include middleware, but the implementation is different.

 

Koa middleware core module is just kernel. The Express includes a complete application framework, with routing templates and other functions. Koa do have options for these features, but they are separate modules. Therefore, a higher degree of modularity of Koa; you only need to include modules required. The core module KOA only about 2,000 lines of code, so if you only need the core request response context object, the Koa occupy very little space. In comparison, Express fairly huge, built a set of middleware capabilities, the benefits for most applications you can save time and choose their own combination of modules.

 

Koa for Express has been extended, and take advantage of the ES7 new syntax. Koa Context object is an extension of the core Express request and response objects, in addition to using the async / await to eliminate callback (callback) trap.

Callback syntax similar to the following:

1
2
3
4
5
6
7
8
9
function  myFunction(params, callback){ 
     //make async call here  
     asyncCall(params,  function (res) { 
         callback(res); 
     }) 
myFunction(myParams,  function (data){   
     //do something with 'data' here 
})

Asynchronous Syntax async / await similar to the following:

1
2
3
4
5
6
7
8
9
10
11
async myFunction(){
     try {
         let res = await asyncCall();
         return  res;
     }
     catch (err){
         console.log( "Error: "  + err);
     }
}
 
let result = await myFunction();

Callback trap (a large number of nested callback function) The problem here skip, wrote JS large-scale applications, should have fully understood, the following async / await grammar in addition to the execution order more clearly, exception handling is also easier. Handling of the problem is both asynchronous and Koa Express a significant difference.

About performance, these two frameworks is almost the same, Koa slightly better, here to skip specific test cases, and basically can reach several thousand req / s.

 

The last brief summary of how the selection:

  1. If it is a new project, the operating environment is no restriction on the ES7 grammar, pay greater attention to the performance, the team has high customization needs and skills, then preference Koa;

  2. If Express is an old project, it is recommended to continue to use the Express, no need to migrate just to come with new technology, Koa and Express is not so compatible;

  3. If you do not want to choose and build a variety of components and modules may have to deal with compatibility issues, and like one of the rapid development of integrated, then select Express, Express is a mature application development framework, and not only provides the core module

Guess you like

Origin www.cnblogs.com/leolovexx/p/12212552.html