mercredi 1 avril 2015

What's the proper way of terminating a Node.js server request?

I'm playing around with a basic Node.js server (no frameworks). I want the server to terminate the request under certain conditions. What's the proper way of doing this? I tried request.connection.destroy(), req.removeListener('data'), and req.removeListener('end'), but the server keeps going and returns results. Here's part of what I have:



http.createServer(function(req,res){
req.on('data',function(data){
content+=data;
if(condition){
req.connection.destroy();
req.removeListener('data');
req.removeListener('end');
}
});

req.on('end',function(){=
res.writeHead(200,{
'Context-Type':'text/plain',
});
res.write(content);
res.end();
});
});


P.S. Isn't it better to do req.once('end',...) instead of req.on('end',...)? Everyone one else seems to use on(), is there a reason for that?


Aucun commentaire:

Enregistrer un commentaire