问一个问题 Node入门 里面有个例子,处理POST请求的时候 可以注册监听器 request.addListener("data", function(chunk) { // called when a new chunk of data was received });
request.addListener("end", function() { // called when all chunks of data have been received });
data事件代表post数据来到 end事件代表数据接收完
但是在程序里怎么知道什么时候出发data事件,什么时候触发end事件呢 代码是 var http = require(“http”); var url = require(“url”);
function start(route, handle) { function onRequest(request, response) { var postData = ""; var pathname = url.parse(request.url).pathname; console.log(“Request for " + pathname + " received.”);
request.setEncoding("utf8");
request.addListener("data", function(postDataChunk) {
postData += postDataChunk;
console.log("Received POST data chunk '"+
postDataChunk + "'.");
});
request.addListener("end", function() {
route(handle, pathname, response, postData);
});
}
http.createServer(onRequest).listen(8888); console.log(“Server has started.”); }
exports.start = start; 难道data和end事件本来就存在?注册了就能用?否则以上面代码看,怎么知道什么时候触发data和end? 如果不是,那事件名字可以随便取吗?