美文网首页
手写use方法

手写use方法

作者: 嵩鼠 | 来源:发表于2020-10-16 16:35 被阅读0次
function express(){
    var funcs = [];
 
    var expr = function(req,res){
        var i = 0;
        function next(){            
            var task = funcs[i++];
            if(!task) return;
            task(req,res,next);
        }
        next();
    }
    expr.use=function(f){
        funcs.push(f);
    }
    return expr;
}
var app = express();
 
app.use(function(req,res,next){
    console.log('haha');
    next();
});
app.use(function(req,res,next){
    console.log('hehe');
    next();
});
app.use(function(req,res){
    res.end("there is nothing happened");
});

引用于:https://blog.csdn.net/u014723529/article/details/42581933?utm_medium=distribute.wap_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-4.wap_blog_relevant_pic&depth_1-utm_source=distribute.wap_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-4.wap_blog_relevant_pic

相关文章

网友评论

      本文标题:手写use方法

      本文链接:https://www.haomeiwen.com/subject/tlazpktx.html