今天学用 koa.js 写个简单的 demo,代码如下:
import koa from 'koa'
let App = koa()
App.use(function *(next) {
const timestampBefore = new Date().getTime()
yield next
const timestampAfter = new Date().getTime()
console.log(`Process Time: ${timestampBefore - timestampAfter}`)
})
App.use(function *() {
this.body = 'Hello from koa'
})
App.listen(4000)
export default App
很简单的一段代码,按照许多教程里描述的,应该是每请求一次就打印一条该请求的处理时间,但是我运行之后却总是一次打印两条请求的处理时间,这两个时间或者相同或者不同并没有规律,更奇怪的是还会出现负数……
这我就想不明白了,谁能给我解释一下里面的道理?谢谢~