新手求问一个Nodejs事件监听问题
新手求问一个Nodejs事件监听问题
问一个问题 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? 如果不是,那事件名字可以随便取吗?
当然可以。你提到的这个问题涉及 Node.js 中的事件驱动编程模式。在 Node.js 中,http
模块的 request
对象是一个事件发射器(EventEmitter),它会发出各种事件,比如 data
和 end
。
什么是事件监听?
事件监听是一种机制,用于在特定事件发生时执行回调函数。例如,在 HTTP 请求中,每当接收到新的数据块时,就会触发 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.on('data', function (postDataChunk) {
postData += postDataChunk;
console.log('Received POST data chunk "' + postDataChunk + '".');
});
// 当所有数据都接收完毕后触发
request.on('end', function () {
route(handle, pathname, response, postData);
});
}
http.createServer(onRequest).listen(8888);
console.log('Server has started.');
}
exports.start = start;
解释
-
引入模块:
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'); // 监听 data 事件 request.on('data', function (postDataChunk) { postData += postDataChunk; console.log('Received POST data chunk "' + postDataChunk + '".'); }); // 监听 end 事件 request.on('end', function () { route(handle, pathname, response, postData); }); }
-
创建服务器并监听端口:
http.createServer(onRequest).listen(8888); console.log('Server has started.'); } exports.start = start;
事件监听的工作原理
request.on('data', callback)
:每当接收到新的数据块时,callback
函数会被调用。request.on('end', callback)
:当所有数据都接收完毕后,callback
函数会被调用。
事件名称是否可以随意取?
不可以。事件名称是固定的,它们由事件发射器(如 request
对象)定义。例如,data
和 end
是 http.IncomingMessage
对象的标准事件。你可以查看文档以了解哪些事件可用。
希望这能帮助你理解 Node.js 中的事件监听机制。如果你有任何其他问题,请随时提问!
data與end是request本身的事件,名字是固定的,是不能改的。當有數據到來的時候data事件就被觸發,數據結束的時候end事件就被觸發。
谢谢 感觉就是写onclick事件那样是吧
那请问能不能写成 request.ondata=function{}呢
不能这样写, node跟前端还是有区别的
谢谢大神解释
在这个问题中,你提到的 data
和 end
事件是 Node.js 中内置的 HTTP 请求对象(http.IncomingMessage
类的实例)自带的事件。这些事件会在接收到请求的数据时被触发,并且在请求结束时触发。
示例代码解析
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");
// 监听 'data' 事件,当接收到新的数据块时被调用
request.on("data", function (postDataChunk) {
postData += postDataChunk;
console.log("Received POST data chunk '" + postDataChunk + "'.");
});
// 监听 'end' 事件,当所有数据块都已接收完毕时被调用
request.on("end", function () {
route(handle, pathname, response, postData);
});
}
http.createServer(onRequest).listen(8888);
console.log("Server has started.");
}
exports.start = start;
解释
-
HTTP 请求对象:每次客户端发起一个请求,Node.js 都会创建一个
http.IncomingMessage
对象。这个对象包含了很多有用的方法和属性,比如读取请求头、请求体等。 -
data 事件:当从客户端接收到新的数据块时,就会触发
data
事件。这些数据通常是以缓冲区的形式传递的,你可以通过setEncoding
方法将其设置为字符串格式。每次接收到新的数据块时,回调函数会被调用,从而将新数据追加到postData
变量中。 -
end 事件:当所有数据块都已经被接收完毕时,
end
事件会被触发。这时,你可以认为请求体已经完整地被接收到了,然后你可以根据需要进行进一步处理。
总结
data
和end
事件是内置的,不是你自己定义的。它们是在接收请求数据过程中自然发生的。- 事件的名字不能随意更改,因为它们是特定于
http.IncomingMessage
对象的。 - 你可以通过监听这些事件来获取和处理请求数据。