鉴于这个简单的网络服务器代码:
console.log('starting');
var server = require('http').createServer();
server.on('connection',function(socket){console.log('*server/connection');});
server.on(
'request',
function(request, response){
console.log('*server/request');
request.on(
'data',
function(chunk){
console.log('*request/data');
// <!> How do I abort next data calls from here?
}
);
request.on(
'readable',
function(chunk){
console.log('*request/readable');
// <!> How do I abort next readable calls from here?
}
);
request.on(
'end',
function(){
console.log('*request/end');
response.writeHead(200,"OK");
response.write('Hello');
response.end();
}
);
request.on('close',function(){ console.log('*request/close'); } );
request.on('error',function(){ console.log('*request/error'); } );
}
);
server.on('close',function(){console.log('server/close');});
server.on('checkContinue',function(request, response){console.log('*server/checkContinue');});
server.on('connect',function(request, socket, head){console.log('*server/connect');});
server.on('upgrade',function(request, socket, head){console.log('*server/upgrade');});
server.on('clientError',function(exception, socket){console.log('*server/clientError');});
server.listen(8080);
console.log('started');
当提交 POST 或 FILE 时,我的 on data 功能会被触发一次或多次或多次。有时(例如发送了一个巨大的大文件)我想在数据事件上取消它并向用户触发 on end 功能(稍后我将显示“您的帖子/文件太大”)。我该怎么做?