3 Star 11 Fork 1

tkoajs / tkoa

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
README.md 2.65 KB
一键复制 编辑 原始数据 按行查看 历史
Bd999 提交于 2019-04-17 10:39 . 更新 README.md

tkoa logo

tkoa build badge tkoa npm badge tkoa npm download badge tkoa gitter badge

🌈 Tkoa is a Koa web app framework written in typescript. typescript logo

Although written in typescript, you can still use the nodejs framework and koa middleware.

Not only that, you can also enjoy the type checking and convenient testing with typescript!

Installation

TKoa requires >= typescript v3.1.0 and node v7.6.0 for ES2015 and async function support.

$ 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 is a middleware framework that can take two different kinds of functions as middleware:

  • async function
  • common function

Here is an example of logger middleware with each of the different functions:

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

  • Higher than version v3.1

Node.js

  • Higher than version v7.6.0

License

MIT

TypeScript
1
https://gitee.com/tkoajs/tkoa.git
git@gitee.com:tkoajs/tkoa.git
tkoajs
tkoa
tkoa
master

搜索帮助