KOA中间件实现原理

 1 //基本原理
 2 var empty=(function *(){})();
 3     //中间件3
 4     var mid2=function *(){
 5     console.log("2:before yield");
 6     yield empty;
 7     console.log("2:after yield");
 8 }
 9 //中间件2
10 var mid1=function *(){
11     console.log("1:before yield");
12     yield *(mid2());
13     console.log("1:after yield");
14 }
15 //中间件1
16 var start=(function *(){
17     console.log("0:before yield");
18     yield *(mid1());
19     console.log("0:after yield");
20 })();
21 while(!(start.next().done)){}

结果
0:before yield
1:before yield
2:before yield
2:after yield
1:after yield
0:after yield


//将数组里面的generator函数compose在一起
var mids=[
function *(next){
    console.log("0:before yield");
    yield 0;
    yield *next;
    yield 1;
    console.log("0:after yield");

},
function *(next){
    console.log("1:before yield");
    yield 2;
    yield *next;
    console.log("1:after yield");
    yield 3;
},
function *(next){
    console.log("2:before yield");
    yield 4;
    yield *next;
    yield 5;
    console.log("2:after yield");
}
];

var next = (function* (){})(),i=mids.length;
/* while (i--) {   next = mids[i].call(null, next);}}*/
for(;i>0;i--){  next=mids[i-1](next);}
var ret;
while(!(ret=next.next()).done){console.log(ret.value);} 

结果
0:before yield
0
1:before yield
2
2:before yield
4
5
2:after yield
1:after yield
3
1
0:after yield

//递归,使用next而非*next跳转到下一个中间件

var mids=[
function *(next){
    console.log("0:before yield");
    yield 0;
    yield next;
    yield 1;
    console.log("0:after yield");
 },
function *(next){
    console.log("1:before yield");
    yield 2;
    yield next;
    console.log("1:after yield");
    yield 3;
},
function *(next){
    console.log("2:before yield");
    yield 4;
    yield next;
    yield 5;
    console.log("2:after yield");
}
];
var next = (function* (){})(),i=mids.length;
for(;i>0;i--){  next=mids[i-1](next);}

function nextGenerator(generatorObj){
    var value,item;
    nextVal();
function nextVal(){
    item=generatorObj.next();
    if(item.done) {return;}
    value=item.value;
    if(isGenerator(value)){
    nextGenerator(value);
}else console.log(value);
    nextVal();
}
function isGenerator(obj) {
    return typeof obj.next==‘function‘ && typeof obj.throw==‘function‘;
}
}
nextGenerator(next);

结果

0:before yield
0
1:before yield
2
2:before yield
4
5
2:after yield
1:after yield
3
1
0:after yield

 1 //第二个中间件不跳转
 2 var mids=[
 3
 4 function *(next){
 5     console.log("0:before yield");
 6     yield 0;
 7     yield next;
 8     yield 1;
 9     console.log("0:after yield");
10 },
11 function *(next){
12     console.log("1:before yield");
13     yield 2;
14     //yield next;
15     console.log("1:after yield");
16     yield 3;
17 },
18 function *(next){
19     console.log("2:before yield");
20     yield 4;
21     yield next;
22     yield 5;
23     console.log("2:after yield");
24 }
25 ];

结果

0:before yield
0
1:before yield
2
1:after yield
3
1
0:after yield



KOA中间件实现原理

时间: 2024-08-06 15:39:12

KOA中间件实现原理的相关文章

Koa中间件(middleware)级联原理

前言 上次看到了koa-compose的代码,今天来说一下koa中间件的级联以及工作原理. 中间件工作原理 初始化koa实例后,我们会用use方法来加载中间件(middleware),会有一个数组来存储中间件,use调用顺序会决定中间件的执行顺序. 每个中间件都是一个函数(不是函数将报错),接收两个参数,第一个是ctx上下文对象,另一个是next函数(由koa-compose定义) 在建立好http服务器后,会调用koa-compose模块对middleware中间件数组进行处理.具体代码这里就

深入解析Koa之核心原理

这篇文章主要介绍了玩转Koa之核心原理分析,本文从封装创建应用程序函数.扩展res和req.中间件实现原理.异常处理的等这几个方面来介绍,写的十分的全面细致,具有一定的参考价值,对此有需要的朋友可以参考学习下.如有不足之处,欢迎批评指正. Koa作为下一代Web开发框架,不仅让我们体验到了async/await语法带来同步方式书写异步代码的酸爽,而且本身简洁的特点,更加利于开发者结合业务本身进行扩展. 本文从以下几个方面解读Koa源码: 封装创建应用程序函数 扩展res和req 中间件实现原理

koa中间件分析

转载请注明: TheViper http://www.cnblogs.com/TheViper  另外可以参考http://purplebamboo.github.io/2014/05/24/koa-source-analytics-3/,作者用简单的方式造了一个山寨koa. koa是什么? koa是从2013年11月开始发布,更新的.和express相比,koa太年轻了.但它(用文档上的话说)通过组合不同的 generator,可以免除重复繁琐的回调函数嵌套,并极大地提升常用错误处理效率.Ko

Koa - 中间件

前言 Koa 应用程序是一个包含一组中间件函数的对象,它是按照类似堆栈的方式组织和执行的. 当一个中间件调用 next() 则该函数暂停并将控制传递给定义的下一个中间件.当在下游没有更多的中间件执行后,堆栈将展开并且每个中间件恢复执行其上游行为. 以上两句话,是我在官方文档中找到其对 Koa 中间件的描述. 在Koa中,中间件是一个很有意思的设计,它处于request和response中间,被用来实现某种功能.像上篇文章所使用的 koa-router .koa-bodyparser 等都是中间件

koa中间件实现分析

最近团队内部做了一个web app,用koa做服务端,一直对他中间件实现很感兴趣,对他的源码研究之后,写了一份简化版本的中间件实现.代码除了用到ES6的Generator和Promise,没有用到其他三方库,总共不到一百行,希望能帮助大家理解! 'use strict'; var middleware = []; //向数据库请求数据 var getDataPromise = new Promise(function(resolve,reject){ setTimeout(function(){

redux中间件的原理——从懵逼到恍然大悟

前言react已经出来很久了,其生态圈之庞大,一锅炖不下!各种react-xx,已让我们不堪重负,github上随便一个demo,引入的模块至少都是五指之数+.看着头疼,嚼之无味…….在此建议新学者,可以从基础的核心模块学起,前期不要考虑那些数量繁多的马仔小弟,边学边写,个人感觉前期核心要学的流程大致如下:React ——> React + redux + React-redux ——> React + redux + React-redux + React-router ——> Rea

傻瓜式解读koa中间件处理模块koa-compose

最近需要单独使用到koa-compose这个模块,虽然使用koa的时候大致知道中间件的执行流程,但是没仔细研究过源码用起来还是不放心(主要是这个模块代码少,多的话也没兴趣去研究了). koa-compose看起来代码少,但是确实绕.闭包,递归,Promise...看了一遍脑子里绕不清楚.看了网上几篇解读文章,都是针对单行代码做解释,还是绕不清楚.最后只好采取一种傻瓜的方式: koa-compose去掉一些注释,类型校验后,源码如下: function compose (middleware) {

nextjs作为koa中间件的使用

react客户端渲染的缺点:首屏速度慢,对SEO不友好 浏览器请求步骤                                                        客户端跳转 1. 浏览器发起请求 /index                                           1.  点击按钮 2. koa接受请求,并且调用nextjs                                 2. 异步加载组件的js 3. nextjs开始渲染   

koa2入门--03.koa中间件以及中间件执行流程

//中间件:先访问app的中间件的执行顺序类似嵌套函数,由外到内,再由内到外 //应用级中间件 const koa = require('koa'); var router = require('koa-router')(); var app = new koa(); //匹配任意路由之前打印日期 app.use(async (ctx,next)=>{ console.log(new Date()); await next(); }); router.get('/',async (ctx)=>