Koa is a lightweight modern middleware framework developed by the Express.js team that allows us to use asynchronous middleware functions.
<code class="language-javascript">const Koa = require('koa'); const app = new Koa(); app.use(async function(ctx) { ctx.body = 'Hello'; }); app.listen(3000);</code>
Here we create a Koa instance and set up a middleware to return "Hello" to each request.
Middleware is a function that receives an HTTP request and may respond to the request during the request/response cycle. It can also call the next middleware in the list. Here is an example.
<code class="language-javascript">async function exampleMiddleware(ctx, next) { // 从 ctx(上下文)读取请求数据 // 更多逻辑 // 设置 ctx.body 作为响应 // 调用下一个中间件 await next(); // 在调用 next 之后,由于使用了 await 语法,我们可以做更多的事情。 // 例如记录所花费的时间 } // 设置中间件 app.use(exampleMiddleware);</code>
Middleware framework allows us to define multiple middleware functions and execute them in the order of definition. You may have noticed the next parameter in the example above. It is a function that, when called, passes control to the next middleware.
Using asynchronous functions allows us to use await to perform certain operations in a stack manner after the middleware is executed.
If no further execution is required, there is no need to call next. For example, consider an authentication middleware. If the user is logged in, next is called, otherwise the login page is displayed.
<code class="language-javascript">// 身份验证中间件 app.use(async (ctx, next) => { if(user){ // 调用列表中的下一个中间件 await next(); } else { // 显示登录页面 } }); // Hello 中间件 app.use(async (ctx) => { ctx.body = 'Hello'; });</code>
As you can see, the order of middleware is also important.
Each middleware receives two parameters. The first is context (abbreviated as ctx) and next.
ctx contains attributes from the request. Additionally, it contains setters that can respond to requests.
The following is a list of common attributes. You can find the full list here.
名称 | 描述 |
---|---|
ctx.method | 当前请求方法 |
ctx.path | 当前请求路径 |
ctx.query | 查询字符串 |
ctx.body | 设置响应正文的设置器 |
Use app.use -document. Please note that the order of middleware is important.
<code class="language-javascript">const Koa = require('koa'); const app = new Koa(); app.use(async function(ctx) { ctx.body = 'Hello'; }); app.listen(3000);</code>
Use app.listen -document.
<code class="language-javascript">async function exampleMiddleware(ctx, next) { // 从 ctx(上下文)读取请求数据 // 更多逻辑 // 设置 ctx.body 作为响应 // 调用下一个中间件 await next(); // 在调用 next 之后,由于使用了 await 语法,我们可以做更多的事情。 // 例如记录所花费的时间 } // 设置中间件 app.use(exampleMiddleware);</code>
Use CTX.BODY setter -document.
<code class="language-javascript">// 身份验证中间件 app.use(async (ctx, next) => { if(user){ // 调用列表中的下一个中间件 await next(); } else { // 显示登录页面 } }); // Hello 中间件 app.use(async (ctx) => { ctx.body = 'Hello'; });</code>
<code class="language-javascript">app.use(middlewareFunction1); app.use(middlewareFunction2); app.use(middlewareFunction3);</code>
How to build a complete application
<code class="language-javascript">app.listen(3000);</code>
To build a complete application, we need to use the middleware provided by the KOA community. The following is a list of common middleware.
router
KOA is a great lightweight modern web framework for building API and web applications. You can read more documents on the KOA website.
The above is the detailed content of Intro to Koa.js: A Lightweight Framework for APIs & Web Apps. For more information, please follow other related articles on the PHP Chinese website!