没啥太多东西,记录下自己实现的思路,还没看源码。之后有机会看下源码比较下实现的差异。
var App = {
index: 0,
functionList:[],
callback (ctx) {
console.log(ctx.name);
},
use (middleware) {
this.functionList.push(middleware);
},
go (ctx = {}) {
let _this = this;
let next = ()=>{
if(_this.index< _this.functionList.length){
_this.index = _this.index+1;
_this.functionList[_this.index-1](ctx,next);
}else{
_this.callback(ctx);
}
};
next();
}
};
App.use(function (ctx, next) {
ctx.name = 'hello';
next();
});
App.use(function (ctx, next) {
ctx.name += ' world';
next();
});
App.go();

本文简单记录了作者尝试模拟实现Express中间件的过程,虽然尚未深入研究官方源码,但分享了个人的实现思路。后续计划对比源码,探讨可能的实现差异。
427

被折叠的 条评论
为什么被折叠?



