代码拉取完成,页面将自动刷新
🌈 Tkoa是使用 typescript 编写的 koa 框架!
尽管它是基于 typescript 编写,但是你依然还是可以使用一些 node.js 框架和基于 koa 的中间件。
不仅如此,你还可以享受 typescript 的类型检查系统和方便地使用 typescript 进行测试!
TKoa 需要 >= typescript v3.1.0 和 node v7.6.0 版本。
$ npm install tkoa
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);
Tkoa 是一个中间件框架,拥有两种中间件:
下面是一个日志记录中间件示例,其中使用了不同的中间件类型:
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`);
});
// 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`);
});
});
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。