我使用此函数在我的 Node.js 应用程序中获取请求的输入数据:
function getData(callback){
req.data = '';
req.receivedDataSize = 0;
req.maxDataSize = 5000;
req.dataCorrupted = false;
req.on('data', function(chunk){
if(req.dataCorrupted === false && chunk.length < req.maxDataSize && req.receivedDataSize < req.maxDataSize){
req.data += chunk;
req.receivedDataChunk += chunk.length;
} else {
// throw 413 error.
req.dataCorrupted = true;
return;
});
req.on('end', function(){
if(req.dataCorrupted === false){
callback(null);
} else {
callback('input-data-corrupted');
}
return;
});
}
我想知道每个事件回调中的这些“return”语句是如何使用的,当我抛出“return”时,下一个“req.on('data')|| req.on('end')”会发生什么? 有没有办法阻止事件侦听器停止侦听该事件,或者只是调用“req.end();” 停止的正确方法?