Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

请教一个关于中间件的运行流程问题,函数被重复执行了; #25

Open
zeyongTsai opened this issue Dec 21, 2015 · 7 comments

Comments

@zeyongTsai
Copy link

some code...

app.use(function *(next){
console.log("-+-+-+-+-+-+");
console.log(this.method);
yield next;
this.body = "hello world world world!";
console.log("------");
});

app.use(route.get('/products',function *(){
console.log("hahaha");
console.log(this.method);
console.log("hehehehe");
}));

app.use(function *(){
console.log('999');
this.body = "hello world!";
console.log(this.method);
console.log("++++++");
});

other code...
控制台的结果:
image

我想请问的是:为什么写在route前面的函数被调用了两次呢?如果放在后面的话,倒是没有这个问题;

@i5ting
Copy link
Contributor

i5ting commented Dec 21, 2015

格式化一下代码

@zeyongTsai
Copy link
Author

哦,不好意思Σ( ° △ °|||)︴
some code...

app.use(function *(next){

console.log("-+-+-+-+-+-+");
console.log(this.method);

yield next; 

this.body = "hello world world world!";
console.log("------");

});

app.use(route.get('/products',function *(){

console.log("hahaha");
console.log(this.method);
console.log("hehehehe");

}));

app.use(function *(){

console.log('999');
this.body = "hello world!";
console.log(this.method);
console.log("++++++");

});

other code...

@hanrea
Copy link
Contributor

hanrea commented Dec 22, 2015

整理了下,感觉你这个是两次请求,只不过打印在一起了。你试试log this.url看请求的路径。

@i5ting
Copy link
Contributor

i5ting commented Dec 22, 2015

好,稍后回复

@iamcc
Copy link
Contributor

iamcc commented Dec 22, 2015

因为浏览器访问了fav.ico

@zeyongTsai
Copy link
Author

@hanrea
@i5ting
@iamcc

恩,确实是两次请求;favicon.ico有单独的请求;

感谢你们的帮助!
冬至快乐!

(づ。◕‿‿◕。)づ

@i5ting
Copy link
Contributor

i5ting commented Dec 22, 2015

@zeyongTsai 添加到faq一节里

@i5ting i5ting reopened this Dec 22, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants