【发布时间】:2018-02-01 17:38:38
【问题描述】:
所以我只是想尝试用 koa 和 koa-router 编写一个 hello-world 服务器。这是我的代码。
const Koa = require('koa')
const Router = require('koa-router')
const timeout = (ms) => new Promise(res => setTimeout(res, ms))
const timestamp = () => {
const d = new Date()
return `${d.getUTCMinutes()}:${d.getUTCSeconds()}`
}
let req = 0
const app = new Koa()
app.use(async (ctx, next) => {
ctx.state.req = req
console.log(`URL: ${ctx.url} time: ${timestamp()} request: ${req++}`)
await next()
})
const router = new Router()
router.get('/', async (ctx, next) => {
ctx.body = {
message: "Hello, World!"
}
await next()
})
router.get('/favicon.ico', async ctx => ctx )
router.get("/test", async ctx => ctx.body = "Just a test")
app.use(router.routes())
app.use(async ctx => {
await timeout(10000)
console.log(`Hit req: ${ctx.state.req}! time: ${timestamp()}`)
ctx.body.message += "After the delay!"
})
app.listen(3000)
console.log(`Listening on port 3000`)
现在这对于来自单个浏览器的单个请求非常有效。当我尝试从同一浏览器向localhost:3000 发出第二个请求时,就会出现问题。在第一个请求完成之前,第二个请求不会被注册(处理?)。如果我向/test 发出请求或使用其他浏览器发出请求,则不会发生这种情况。 Chrome 和 Firefox 的行为是一致的。唯一的例外是 Firefox Quantum。
Listening on port 3000
URL: / time: 47:5 request: 0
URL: / time: 47:7 request: 1
Hit req: 0! time: 47:15
Hit req: 1! time: 47:17
URL: / time: 47:21 request: 2
Hit req: 2! time: 47:31
URL: / time: 47:31 request: 3
Hit req: 3! time: 47:41
URL: / time: 58:14 request: 4
Hit req: 4! time: 58:24
URL: / time: 58:47 request: 5
URL: / time: 58:50 request: 6
Hit req: 5! time: 58:57
Hit req: 6! time: 59:0
这里的请求 0 和 1 来自两个不同的浏览器。如您所见,请求 1 与请求 0 的状态无关。请求 2 和 3 来自同一个浏览器(Chrome、Firefox 或 Opera,它们每个都相同)。这里请求 3 仅在服务器完成对请求 2 的响应后才被注册。请求 4 是一个孤立的请求。请求 5 和 6 来自 Firefox Quantum,它的行为符合预期。
【问题讨论】:
-
也许浏览器只想使用单个连接?不过,我认为限制高于 1。
-
@Ryan 什么意思,TCP 连接到服务器?即使这样,浏览器不应该能够通过管道在一个连接上发送多个请求吗?
-
是的,我指的是 TCP 连接。您是正确的,浏览器应该能够通过管道发送多个请求,即使在单个连接上也是如此,但您确定它是吗?我知道随着 HTTP/2 的出现,Firefox 至少已经取消了它的 HTTP/1.1 流水线偏好。也不知道 Chrome 是否曾经默认启用它。
-
问题在于调用 await next 而不是 next() 而不是浏览器
-
@DanielStaleiny 但是,根据 Koa 文档,使用
await调用它非常好。在这里,我将代码精简到最低限度,这使得await看起来多余,但我需要在我的代码中使用它,因为我希望控件针对每个请求向上游流动。 github.com/koajs/koa
标签: node.js google-chrome firefox asynchronous koa