Skip to content

ydyoto/koa-mini

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 

Repository files navigation

koa-mini

Application

Context

Middleware

难点在于洋葱模型的理解,以及各个中间件的组合使用。 在执行上使用一个递归函数,将下一个中间件作为参数传入 next 中。

递归函数定义如下:

const dispatch = (i) => {
	if (i === middlewares.length) {
		return ;
	}

	const middleware = middlewares[i];
	return middleware(ctx, () => dispatch(i + 1));
}

完整的组合方法为:

function compose (middlewares) {
	return (ctx) => {
		const dispatch = (i) => {
			if (i === middlewares.length) {
				return ;
			}

			const middleware = middlewares[i];
			return middleware(ctx, () => dispatch(i + 1));
		}
		return dispatch(0);
	}
}

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published