/
2.ts
38 lines (31 loc) · 947 Bytes
/
2.ts
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
import * as Koa from 'koa';
import * as supertest from 'supertest';
const app = new Koa();
//最佳实践,中间件应接受可选参数options,如下面logger中的format参数
function logger(format: String = '') {//最佳实践:中间件最好有名字(不用匿名函数)
format = format || ':method | :url';
return async function (ctx: Koa.Context, next: Function) {
const str = format
.replace(':method', ctx.method)
.replace(':url', ctx.url);
if (ctx.body) {
ctx.body += ' ' + str;
} else {
ctx.body = str;
}
await next();
};
}
app.use(logger(':method & :url'));
app.listen(3000);
supertest('http://localhost:3000')
.get('/test')
.expect(200)
.expect('GET & /test')
.end((err, res) => {
if (err) {
throw err;
} else {
console.log('测试通过!');
}
});