2013-08-17 07:15:57 +00:00
|
|
|
|
2015-10-11 22:59:51 +00:00
|
|
|
'use strict';
|
|
|
|
|
2015-10-13 06:19:42 +00:00
|
|
|
const Koa = require('..');
|
|
|
|
const app = new Koa();
|
2013-08-17 07:15:57 +00:00
|
|
|
|
|
|
|
// number of middleware
|
|
|
|
|
2015-10-11 23:24:42 +00:00
|
|
|
let n = parseInt(process.env.MW || '1', 10);
|
2017-08-03 01:33:51 +00:00
|
|
|
let useAsync = process.env.USE_ASYNC === 'true';
|
|
|
|
|
|
|
|
console.log(` ${n}${useAsync ? ' async' : ''} middleware`);
|
2013-08-17 07:15:57 +00:00
|
|
|
|
|
|
|
while (n--) {
|
2017-08-03 01:33:51 +00:00
|
|
|
if (useAsync) {
|
2019-06-26 03:15:22 +00:00
|
|
|
app.use(async(ctx, next) => await next());
|
2017-08-03 01:33:51 +00:00
|
|
|
} else {
|
|
|
|
app.use((ctx, next) => next());
|
|
|
|
}
|
2013-08-17 07:15:57 +00:00
|
|
|
}
|
|
|
|
|
2017-03-20 06:48:37 +00:00
|
|
|
const body = Buffer.from('Hello World');
|
2013-08-17 07:15:57 +00:00
|
|
|
|
2017-08-03 01:33:51 +00:00
|
|
|
if (useAsync) {
|
2019-06-26 03:15:22 +00:00
|
|
|
app.use(async(ctx, next) => { await next(); ctx.body = body; });
|
2017-08-03 01:33:51 +00:00
|
|
|
} else {
|
|
|
|
app.use((ctx, next) => next().then(() => ctx.body = body));
|
|
|
|
}
|
2013-08-17 07:15:57 +00:00
|
|
|
|
2013-08-28 23:26:58 +00:00
|
|
|
app.listen(3333);
|