Koa正在执行两次每个请求?

Koa是否有任何理由为每个请求执行两次

const Koa = require('koa') const app = new Koa() const index = async(ctx, next) => { console.log('Hello world!') await next() ctx.body = 'Hello world!' } app.use(index); app.listen(3000) 

在我的terminal上,我得到:

 Hello world! Hello world! 

有任何想法吗?

这可能发生的原因有两个:

首先是 – 正如在评论中已经提到的那样,浏览器也发出了对favicon.ico的请求。第二:一些浏览器做了一个优化,所以在你点击返回键之前,他们预先inputurl。

 const Koa = require('koa') const app = new Koa() const index = async(ctx, next) => { console.log('URL --> ' + ctx.request.url); // This logs out the requested route console.log('Hello world!') await next() ctx.body = 'Hello world!' } app.use(index); app.listen(3000) 

我在代码中添加了一行代码,以便您可以查看浏览器要求的路由。 这可能有助于确定问题的原因。