对于http.createServer([requestListener]),我们最常用的用法无怪乎: http.createServer(function(request, response) { //针对不同url的处理代码 }); 这里假设在某个url A对应处理代码中很耗时,这时候你请求A地址的时候肯定会使浏览器处于一直加载状态,这时候你再打开一个页面,然后去请求url B,假设B地址的代码仅仅是现实一个静态页,然后你会发现B地址也处于一直加载状态,也就是说对于url A的耗时处理代码把整个node进程都给堵塞了。 但是我看了一下node中的文档: http.createServer([requestListener])# Returns a new web server object.
The requestListener is a function which is automatically added to the 'request' event. 人家说requestListener会被添加到request事件中。http.createServer会返回一个http.Server,而这个类继承自EventEmitter,含有request事件监听。对于这个request事件,文档中是这么写的: Event: 'request'# function (request, response) { }
Emitted each time there is a request. Note that there may be multiple requests per connection (in the case of keep-alive connections). request is an instance of http.IncomingMessage and response is an instance of http.ServerResponse 看完了之后,我依然无法理解之前的那个堵塞现象发生的原因是什么,请各位大牛指教。