How to close a request in nodejs?

What I have:

I use node.js with express framework , and I create where I can upload files, I use node-formidable for this.

What a problem

My problem is that I cannot close the request if I find an error with the downloaded files. I want to check file types, size, etc. Therefore, I can upload the files I need, and the files are not actually downloaded, so I do not waste time.

So the problem is that I cannot stop the HTTP request.

What i tried

Here is what I have tried so far:

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

I assume that connection.destroy() interrupts the request, and I know this because it raises the form interrupt event form.on('abort', function(){ ... }) ). But the file is still loading, and the response does not arrive immediately after the file has been downloaded.

So, how do I close the HTTP request and send a message to the client?

EDIT: And something else, when I do not use response.end() , then it works, except that the Client is waiting for a response, this is strange: \

+7
source share
2 answers

The correct way to do this is:

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

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

+2
source

Try with the following code:

  • When server-side validation fails, give an answer like:

    res.json({'IsFileUploaded':'false', 'requiredFileSize': '1000bytes', 'actualFileSize': '800bytes', 'AlertMsg':'File size should be at least 1000 bytes....'})

    It closes the connection, and also you can transfer your data in JSON format

0
source

All Articles