Handling multiple parallel HTTP requests in Node.js

后端 未结 4 594
死守一世寂寞
死守一世寂寞 2021-01-07 19:12

I know that Node is non-blocking, but I just realized that the default behaviour of http.listen(8000) means that all HTTP requests are handled one-at-a-time. I

4条回答
  •  一向
    一向 (楼主)
    2021-01-07 19:26

    Your code can accept multiple connections because the job is done in callback function of the setTimeout call.

    But if you instead of setTimeout do a heavy job... then it is true that node.js will not accept other multiple connections! SetTimeout accidentally frees the process so the node.js can accept other jobs and you code is executed in other "thread".

    I don't know which is the correct way to implement this. But this is how it seems to work.

提交回复
热议问题