我是 Node.js 新手。我想知道这两段代码有什么区别:
var http = require("http");
http.createServer(function(request,response) {
request.addListener("end", function(){
console.log(request);
});
}).listen(8888);
和
var http = require("http");
http.createServer(function(request,response) {
console.log(request);
}).listen(8888);
也就是说,既然每次服务器接收完数据都会触发end
事件,为什么还要使用它呢?新手问题。
最佳答案
我不是 NodeJS 专家,但以下内容是从文档中逻辑得出的。
考虑上传大文件的请求。当请求首次到达服务器时,将调用您传递给 createServer
的回调;当请求完全发送时,request
对象(继承自 ReadableStream
)上的 end
事件将触发。那将是相当不同的时代。
关于javascript - 有或没有请求结束事件,有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18398910/