Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to close a request in nodejs?

What I have:

I'm using node.js with the express framework and I'm building a form where I can upload files, I'm using node-formidable for this.

What's the problem

My problem is that I can't close the request if I found an error with the uploaded files. I want to check file types, size etc.. So I can get the proper files uploaded, and the files are actually not uploaded so I don't waste time.

So the problem is that I can't stop the HTTP request.

What I have tried

Here is what I have tried so far:

request.connection.destroy();
response.end('something went wrong...');

I assume that the connection.destroy() aborts the request, and I know this because it fires the formidables form abort event (form.on('abort', function(){ ... })) But the file is still uploading, and the response doesn't arrives just after the file was uploaded.

So how should I close the HTTP Request, and send a message back to the client?

EDIT: And something else, when I don't use response.end() then it works, except that the Client waits for the the answer, it's weird :\

like image 292
Adam Halasz Avatar asked Aug 26 '11 20:08

Adam Halasz


1 Answers

The correct way for doing this is:

request.pause();
response.status = 400;
response.end('something went wrong...');

Source: https://groups.google.com/forum/?fromgroups=#!topic/nodejs/2gIsWPj43lI

like image 86
andresgottlieb Avatar answered Oct 24 '22 23:10

andresgottlieb