-
Notifications
You must be signed in to change notification settings - Fork 4
/
README.md
99 lines (75 loc) · 2.67 KB
/
README.md
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
![tkoa logo](https://raw.githubusercontent.com/tkoajs/tkoa/master/source/logo.png)
<p align="center">
<img alt='tkoa build badge' src='https://travis-ci.org/tkoajs/tkoa.svg?branch=master'>
<img alt='tkoa npm badge' src='https://img.shields.io/npm/v/tkoa.svg'>
<img alt='tkoa npm download badge' src='https://img.shields.io/npm/dm/tkoa.svg'>
<a href='https://gitter.im/tkoa-js/community?utm_source=share-link&utm_medium=link&utm_campaign=share-link'><img alt='tkoa gitter badge' src='https://badges.gitter.im/tkoa-js/community.svg'></a>
</p>
🌈 Tkoa is a Koa web app framework written in typescript. ![typescript logo](https://raw.githubusercontent.com/tkoajs/tkoa/master/source/ts%20logo.png)
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.
```shell
$ npm install tkoa
```
### Hello T-koa
```typescript
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+):
```typescript
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
```typescript
// 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
- [Tkoa - wiki](https://github.com/tkoajs/tkoa/wiki)
- [zhcn - 中文文档](https://github.com/tkoajs/tkoa/blob/master/README_CN.md)
- [Gitter](https://gitter.im/tkoa-js/community)
## Support
### TypeScript
- Higher than version v3.1
### Node.js
- Higher than version v7.6.0
## License
[MIT](https://github.com/tkoajs/tkoa/blob/master/LICENSE)