<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??碼云GVP開源項目 12k star Uniapp+ElementUI 功能強大 支持多語言、二開方便! 廣告
                # 簡介 Koa 是一個web 框架,由 Express 幕后的原班人馬打造, 致力于成為 web 應用和 API 開發領域中的一個更小、更富有表現力、更健壯的基石。 通過利用 async 函數,Koa 幫你丟棄回調函數,并有力地增強錯誤處理。 Koa 并沒有捆綁任何中間件, 而是提供了一套優雅的方法,幫助您快速而愉快地編寫服務端應用程序。 # 應用程序 Koa 應用程序是一個包含一組中間件函數的對象,它是按照類似堆棧的方式組織和執行的。 Koa 類似于其他中間件系統,然而,一個關鍵的設計點是在其低級中間件層中提供高級“語法糖”。 盡管提供了相當多的有用的方法 Koa 仍保持了一個很小的體積,因為沒有捆綁中間件。 必修的 hello world 應用: ~~~js const Koa = require('koa'); const app = new Koa(); app.use(async ctx => { ctx.body = 'Hello World'; }); app.listen(3000); ~~~ # 級聯 Koa 中間件以更傳統的方式級聯,使用 async 功能,我們可以實現 “真實” 的中間件。 Koa中間件包含兩個參數?ctx和next。參數next是一個函數,它的作用是將處理控制權交給下一中間件。Koa中間件采用級聯代碼方式執行。其中間件參數next的級聯執行邏輯如圖所示。 **Koa中間件執行順序原理圖** ![](https://box.kancloud.cn/fd4831628f2d9c70a2b5b45738343324_713x400.png) 下面以 “Hello World” 的響應作為示例,當請求開始時首先請求流通過`x-response-time`和`logger`中間件,然后繼續移交控制給`response`中間件。當一個中間件調用`next()`則該函數暫停并將控制傳遞給定義的下一個中間件。當在下游沒有更多的中間件執行后,堆棧將展開并且每個中間件恢復執行其上游行為。 > 中間件的執行有先后順序。 ~~~js const Koa = require('koa'); const app = new Koa(); // 中間件 1 app.use(async (ctx, next) => { await next(); const rt = ctx.response.get('X-Response-Time'); console.log(`${ctx.method} ${ctx.url} - ${rt}`); }); // 中間件 2 app.use(async (ctx, next) => { const start = Date.now(); await next(); const ms = Date.now() - start; ctx.set('X-Response-Time', `${ms}ms`); }); // 中間件 3 app.use(async (ctx, next) => { ctx.body = 'Hello World'; }); app.listen(3000, () => {//監聽3000端口 console.log('server is running at http://localhost:3000') }); ~~~
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看