TKoa 1.0.1 发布,TypeScript 版的 Node.js Koa 框架

栏目: 软件资讯 · 发布时间: 6年前

内容简介:更新信息: npm增加编译好的 javascript 文件 T-Koa 介绍: Tkoa是使用 typescript 编写的 koa 框架! 尽管它是基于 typescript 编写,但是你依然还是可以使用一些 node.js 框架和基于 koa 的中间件。 不仅如此,...

更新信息:

  1. npm增加编译好的 javascript 文件

T-Koa 介绍:

TKoa 1.0.1 发布,TypeScript 版的 Node.js Koa 框架

Tkoa是使用 typescript 编写的 koa 框架! TKoa 1.0.1 发布,TypeScript 版的 Node.js 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

  • 大于等于 v3.1 版本

Node.js

  • 大于等于 v7.6.0 版本

License

MIT


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

最愚蠢的一代

最愚蠢的一代

马克·鲍尔莱因 / 杨蕾 / 天津社会科学院出版社 / 2011-7 / 39.80元

《最愚蠢的一代》 美国大学教授的鲍尔莱恩认为,数码时代正在使美国的年轻一代成为知识最贫乏的一代人。 美国的青少年和年轻人正在被数码时代各种娱乐消遣性的工具所淹没。这些工具包括手机、社交网络和信息传送等等。他们通过这些工具传达的却是幼稚浮浅的东西,而且这些东西正在妨碍他们同历史、公民义务、国际事务和美术等成年人的现实世界进行重要的接触。 我们想当然地以为,这些善于吸收新技术的美国年......一起来看看 《最愚蠢的一代》 这本书的介绍吧!

XML、JSON 在线转换
XML、JSON 在线转换

在线XML、JSON转换工具

XML 在线格式化
XML 在线格式化

在线 XML 格式化压缩工具

RGB CMYK 转换工具
RGB CMYK 转换工具

RGB CMYK 互转工具