难以理解NodeJS中的callback方式

这是nodechool.io冒险课程练习的解决scheme。

我很难理解线路是如何工作的

req.pipe(upper).pipe(res) 

内部会发生什么事情的深刻解释将不胜感激。

据我了解,请求将其信息发送到上层进行转换,但是当它通过callback返回时,第二个pipe道是否在返回值上调用?

 var http = require('http'); var fs = require('fs'); var through2 = require('through2'); var upper = through2(function(data, _, next) { data = data.toString().toUpperCase(); next(null,data); }); http.createServer(function(req,res) { if (req.method == 'POST') { req.pipe(upper).pipe(res); } }).listen(process.argv[2]); 

在更多或更less的情况下,

 req.on('data', function(chunk) { next_stream.write(chunk); }); // also handle the 'end' and 'close' events 

并返回next_stream ,所以.pipe可以被链接。 res是一个可写入的stream,表示浏览器读取的stream,只需要写入和closures响应,然后将其传递给浏览器。

我想没有什么值得注意的是,在这种情况下,没有一个被称为“完成”响应的“callback”。 浏览器只是在我们将数据写入res获取数据,并在resclosures时停止加载更多的数据。

查看.pipe方法的.pipe源代码