<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          TKoa基于 TypeScript 的 Node.js koa框架

          聯(lián)合創(chuàng)作 · 2023-09-20 03:42

          tkoa logo

          Tkoa是使用 typescript 編寫的 koa 框架! typescript logo

          盡管它是基于 typescript 編寫,但是你依然還是可以使用一些 node.js 框架和基于 koa 的中間件。

          不僅如此,你還可以享受 typescript 的類型檢查系統(tǒng)和方便地使用 typescript 進行測試!

          安裝

          TKoa 需要 >= typescript v3.1.0 和 node v7.6.0 版本。

          $ npm install tkoa

          Hello T-koa

          import tKoa = require('tkoa');
          
          interface ctx {
              res: {
                  end: Function
              }
          }
          
          const app = new tKoa();
          
          // response
          app.use((ctx: ctx) => {
              ctx.res.end('Hello T-koa!');
          });
          
          app.listen(3000);

          Middleware

          Tkoa 是一個中間件框架,擁有兩種中間件:

          • 異步中間件
          • 普通中間件

          下面是一個日志記錄中間件示例,其中使用了不同的中間件類型:

          async functions (node v7.6+):

          interface ctx {
              method: string,
              url: string
          }
          
          app.use(async (ctx: ctx, next: Function) => {
              const start = Date.now();
              await next();
              const ms = Date.now() - start;
              console.log(`${ctx.method} ${ctx.url} - ${ms}ms`);
          });

          Common function

          // Middleware normally takes two parameters (ctx, next), ctx is the context for one request,
          // next is a function that is invoked to execute the downstream middleware. It returns a Promise with a then function for running code after completion.
          
          interface ctx {
              method: string,
              url: string
          }
          
          app.use((ctx: ctx, next: Function) => {
              const start = Date.now();
              return next().then(() => {
                  const ms = Date.now() - start;
                  console.log(`${ctx.method} ${ctx.url} - ${ms}ms`);
              });
          });

          Getting started

          Support

          TypeScript

          • 大于等于 v3.1 版本

          Node.js

          • 大于等于 v7.6.0 版本

          License

          MIT

          瀏覽 10
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          編輯 分享
          舉報
          評論
          圖片
          表情
          推薦
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          編輯 分享
          舉報
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  在线看黄片网站 | 荫蒂添到高潮免费视频 | 密芽网站黄| 青娱乐成人视频 | 激情五月丁香色婷婷 |