Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use koa in nodejs

2025-01-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article focuses on "how to use koa in nodejs". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let the editor take you to learn how to use koa in nodejs.

Koa refers to a Node-based web framework similar to Express, which aims to become a smaller, more expressive and robust cornerstone in the field of web applications and API development. Instead of bundling any middleware, Koa provides an elegant way to help users write server-side applications quickly and happily.

The operating environment of this tutorial: windows7 system, nodejs 12.19.0&&koa2.0 version, Dell G3 computer.

Koa is a Web development framework similar to Express, and its founder is the same person. Its main feature is that it uses the Generator function of ES6 to redesign the architecture. In other words, the principle and internal structure of Koa are very similar to Express, but the syntax and internal structure have been upgraded.

Koa is a new web framework built by the people behind Express to become a smaller, more expressive, and more robust cornerstone in the field of web applications and API development. By using the async function, Koa helps you discard the callback function and powerfully enhance error handling. Instead of bundling any middleware, Koa provides an elegant way to help you write server-side applications quickly and happily.

The official faq has a question: "Why isn't koa Express 4.0?" The answer is: "Koa is very different from Express, and the whole design is different, so upgrading Express 3.0 to 4.0 in this way means rewriting the entire program. Therefore, we think it is more appropriate to create a new library."

1 Koa application

A Koa application is an object that contains an array of middleware, which consists of a set of Generator functions. These functions are responsible for processing HTTP requests, such as generating caches, specifying proxies, redirecting requests, and so on.

Var koa = require ('koa'); var app = koa (); app.use (function * () {this.body =' Hello World';}); app.listen (3000)

In the above code, the variable app is a Koa application. It listens on port 3000 and returns a web page with a content of Hello World.

The app.use method is used to add Generator functions to the middleware array.

The listen method specifies the listening port and starts the current application.

It is actually equivalent to the following code.

Var http = require ('http'); var koa = require (' koa'); var app = koa (); http.createServer (app.callback ()). Middleware (3000)

Koa's middleware is much like Express's middleware, which is also a function for handling HTTP requests, but it must be a Generator function.

Moreover, the middleware of Koa is a Cascading structure, that is, it is called layer by layer, the first middleware calls the second middleware, the second invokes the third, and so on. The upstream middleware will not continue to execute until the downstream middleware returns the result, which is very similar to recursion.

The middleware is registered through the currently applied use method.

App.use (function* (next) {var start = new Date; / / (1) yield next; / / (2) var ms = new Date-start; / (3) console.log ('% s -% slots, this.method, this.url, ms); / / (4)})

In the above code, the parameter of the app.use method is the middleware, which is a Generator function. The biggest feature is that there must be an asterisk between the function command and the parameter. The parameter next of the Generator function represents the next middleware.

The yield command is used inside the Generator function to transfer the execution power of the program to the next middleware, namely yield next, and the execution will not continue until the next middleware returns the result.

In the above code, inside the body of the Generator function, the first line of the assignment statement is executed first, and the timing starts.

The second line of the yield statement transfers the execution to the next middleware, and the current middleware suspends execution.

When the execution of the later middleware is completed, the execution power will return to the place where it was originally suspended and continue to execute, and then the third line will be executed.

Calculate how long the process took, which is printed out on the fourth line.

The following is an example of two middleware cascades.

App.use (function * () {this.body = "header\ n"; yield saveResults.call (this); this.body + = "footer\ n";}); function * saveResults () {this.body + = "Results Saved!\ n";}

In the above code, the first middleware invokes the second middleware saveResults, and they all write to this.body. Finally, the output of this.body is as follows.

HeaderResults Saved!footer

It should be noted that as long as one middleware lacks a yield next statement, the subsequent middleware will not be executed.

App.use (function * (next) {console.log ('> one'); yield next; console.log ('> two'); this.body = 'two'; console.log (' > three'); yield next; console.log ('

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report