TKoa - 使用 TypeScript 重构的 Node.js Koa 开发框架

2019-04-17 14:48:12 +08:00
 18510047382

🌈Tkoa 是使用 typescript 编写的 koa 框架!

尽管它是基于 typescript 编写,但是你依然还是可以使用一些 node.js 框架和基于 koa 的中间件。

不仅如此,你还可以享受 typescript 的类型检查系统和方便地使用 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

Node.js

License

MIT

5921 次点击
所在节点    Node.js
16 条回复
siloong
2019-04-17 15:49:38 +08:00
支持。btw,为什么不叫 Toa。。。
solome
2019-04-17 16:19:00 +08:00
不理解这样做的意义(分割),不是已经存在`@types/koa`了么。
askfermi
2019-04-17 16:20:53 +08:00
同楼上问
maichael
2019-04-17 16:21:58 +08:00
同问,跟原有的 Koa + types 有什么区别。
18510047382
2019-04-17 16:27:59 +08:00
@solome @types/koa 只是 koa 添加到 TypeScript 的一个语法包而已,只是提供给 TypeScript 一些语法的信息,但是 koa 这个框架并没有 TypeScript 版本的,这两者是不一样的
18510047382
2019-04-17 16:33:05 +08:00
@askfermi 已经给楼上回答了。
@maichael 已经给楼上回答了。
Axurez
2019-04-17 17:29:57 +08:00
为啥还在用 `import tKoa = require('tkoa');` 这种写法
18510047382
2019-04-17 17:53:18 +08:00
@Axurez 文档说明里已经写明了,这样用 commonjs 模块规范是为了更好地兼容 koa 中间件和 node.js 框架。
meteor957
2019-04-17 23:46:06 +08:00
赞。不过现在有 nestjs 了,用 ts 封装了 express.不知道 i 和楼主这个区别大不大
18510047382
2019-04-18 07:41:10 +08:00
@meteor957 其实就是 koa 和 Express 的差别
Gea
2019-04-18 10:22:35 +08:00
typescript 版本的 koa 有什么优势吗,nest 我记得好像还有些注解的语法糖,用起来和 ng、sprintboot 很像
18510047382
2019-04-18 10:44:55 +08:00
@Gea 拥有 TypeScript 完整的类型检查系统,可以很方便的调试
18510047382
2019-04-18 11:36:57 +08:00
@siloong 都一样咯
blanu
2019-05-08 04:29:43 +08:00
内部用 TS 实现和外部暴露 API 用 TS 写 Definition 差不多吧……
blanu
2019-05-08 12:30:54 +08:00
今天又看了下,类型推断竟然是要你自己写 interface,要你这个有何用?
还有什么用 legacy 的 import 语法说是为了兼容 commonjs,我佛了,tsconfig 里面不会自己设置?
这就是个骗🌟项目
cl903254852
2019-05-22 14:47:44 +08:00
不是有 @types/koa 吗?
而且 import tKoa = require('tkoa'); 是什么写法。。。 没明白

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/556041

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX