Before using node to build backstage are used express, but the company's recent projects with KOA, after work to understand.
First, Environment construction
1, a new JS file named App.js, the code is as follows:
// Import Koa, unlike Koa 1.x, in Koa2, we import a class, so the uppercase Koa means:const KOA = require (' Koa '); // Create a Koa object that represents the Web App itself: New Koa (); // for any request, the app will call the asynchronous function to process the request:App.use (CTX, next) = { await next () ; = ' text/html '; = ' ;}); // on Port 3000 listen:app.listen (console.log); (' app started at Port 3000 ... ');
2, then the question comes, how to correctly introduce KOA this module? The quicker way is to direct NPM
NPM install [email protected]
Second, visit
Node app
To create an HTTP service and listen on Port 3000, we can see Hello koa2! when we visit localhost:3000
Iii. explanations
KOA was created by Express's original squad and is committed to becoming a smaller, more expressive, and robust WEB framework. Writing Web applications using KOA, by combining different generator, eliminates the duplication of tedious callback function nesting and greatly improves the efficiency of error handling. KOA does not bind any middleware in the kernel method, it simply provides a lightweight and elegant library of functions, making it handy to write WEB applications. --Excerpt from KOA official documentation
Node constructs the KOA of the web framework