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

What are the main knowledge points of koa middleware

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

Share

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

This article introduces the relevant knowledge of "what are the main knowledge points of koa middleware". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Why the next function must and can only be called once in koa middleware? we can see that if next is not called, dispatch will not be triggered, and the next middleware will not be triggered, resulting in a timeout of the final request in the suspended state.

Koa middleware mainly involves several knowledge points of Promise:

The async function returns a Promise object [all middleware returns a promise object]

When the async function encounters an await call, it pauses the execution of the await function, waits for the result to be returned, and then continues to execute downwards.

An error within the async function will cause the returned Promise to become reject.

Now let's review a few questions raised earlier:

Why does the call to next () need to add await?

This is also the core of the onion ring invocation mechanism. When awaitnext () is executed, it executes next () [calls the next middleware] to wait for the result to be returned, and then executes downwards.

How to provide koa middleware with Context

Goal: encapsulate Context and provide convenient operation mode of request and response

/ / 1. Define KoaRequest, KoaResponse, KoaContext

InterfaceKoaContext {

Request?:KoaRequest

Response?:KoaResponse

Body:String | null

}

Constcontext:KoaContext= {

Getbody () {

Returnthis.response!.body

}

Setbody (body) {

This.response.bodykeeper body

}

}

FunctioncomposeMiddleware (middlewares:middlewareFn []) {

Return (context:KoaContext) = > {

Letstart=-1

Functiondispatch (i:number) {

/ /.. Omit other codes..

/ / 2. All middleware accept context parameters

Middleware (context, () = > {

Returndispatch (iTun1)

})

}

Returndispatch (0)

}

}

This is the end of the content of "what are the main knowledge points of koa middleware". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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