suemi
suemi

Reputation: 69

How to handle undefined upload in NodeJS

I use BusBoy plugin to handle file upload. It registers a handler for event 'file' which is happening when the file has been uploaded completely,then the handler will do the next operation. But when the upload file is undefined,event handler is registered but event 'file' never happens. It will take along time for process to stop handling this request. When I keep on doing such thing for more than five times,the node process will halt for several minutes, it will come to normal after a few minutes.

Then how can I handle this situation in NodeJS process? Don't tell me never to upload undefined, when file upload failed, I think such situation will also take place.

Upvotes: 0

Views: 98

Answers (2)

suemi
suemi

Reputation: 69

I have solved this problem through this. Set a variable flag for 'file' event, when the event happened the flag will be set true in its handler. Because 'file' event happens before req.end event,so I register a handler for the end event of req.

req.on('end',function(){
    if(!flag){
         res.json({
            ....        
         });
    }

});

Upvotes: 0

codeofnode
codeofnode

Reputation: 18619

you can use 'finish' event to stop processing

busboy.on('finish', function() {
 //your code to stop processing
});

This 'finish' event will trigger when all files/fields events in the uploading (form) data have been triggered. (Trigger is ignored if file/field is undefined).

further it is recommended to recheck in your case to pipe your node request through busboy

req.pipe(busboy);

Upvotes: 1

Related Questions