So I am just to trying to write a hello-world server with koa and koa-router. This is my code.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`) Now this is working perfectly for a single request from a single browser. Problem arises when I try to make a second request to localhost:3000 from the same browser. The second request doesn't get registered (handled?) until the first request has been completed. This doesn't happen if I make a request to /test or use another browser to make the request. The behaviour is consistent for both Chrome and Firefox. Only exception is 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 Here request 0 and 1 are from two different browsers. As you can see, request 1, irrespective of request 0's status. Request 2 and 3 are made from same browser (either Chrome, Firefox or Opera, its same for each of them). Here request 3 only gets registered after the server has finished responding to request 2. Request 4 was an isolated request. Request 5 and 6 are from Firefox Quantum and it behaves as expected.Stackoverflow question
Submitted February 01, 2018 at 05:39PM by sioa
No comments:
Post a Comment