Node constructs the KOA of the web framework

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.