2017-04-06 43 views
1

你好,我目前使用的网站“Codeschool.com”,我觉得教官代码中有一个无限循环中的节点/ Javascript代码,但我不能肯定。这个While循环如何结束? - 节点编号

下面是代码:

http.createServer(function(request, response) { 
    response.writeHead(200); 
    request.on('readable', function() { 
    var chunk = null; 
    while(null !==(chunk = request.read())) { 
     response.write(chunk); 
    } 
    }); 
    request.on('end', function() { 
    response.end(); 
    }); 
}).listen(8080) 

教员说,这个代码是类似于使用request.pipe(响应);

我理解这个概念,但什么扔我一个循环(没有双关语意)是这个while循环,它是如何了得?

+1

当'request.read()' null'分配''到chunk',条件不满足,它停止,因为它是现在'=== null'。 – 2017-04-06 00:11:40

回答

0

request.read()每次调用时会返回一部分可用数据,直到没有更多数据要返回为止,在这种情况下返回null

在这一点上,chunk也等于null并且由于分配是返回所分配的值的表达式,条件为假,并为此while循环结束时:

while (null !== (chunk = null)) { ... }