How to send only 200 response in node.js

I have node.js 5.2.0, expression 4.2.0 and formidable 1.0.17.

I created a simple form to save a text box and a photo. It works fine, but the problem is that after loading the data, I see on the console that the POST is not completed, it is still waiting.

To finish, I added this to my code

form.on('end', function() { res.writeHead(200, {'Content-Type': 'text/plain'}); }); 

I just want to send headers and nothing on the page. I want the system to get a 200 ok response without printing anything on the page. But the POST is still pending.

How to fix it without typing anything? What headlines should I send?

thanks

UPDATE

If i do

 form.on('end', function() { res.end(); }); 

POST completes normally, but I get a blank page. Why is this? I just want to load some things, not print anything on the page, and not redirect, stay on one page.

Thanks again

+5
source share
1 answer

Try this instead:

 res.sendStatus(200); 

Or, if you want to continue using explicitly defined headers, I believe that res.end() needs to be called at some point. You can see how res.end() used in an Unusual example .

A blank page is most likely the result of processing your client form. You can override the form submission method and manually submit to your express service to prevent the automatic redirection that you see. Here are a few other fooobar.com/questions/319325 / ... to the question related to form redirection. The answers are specific to jQuery, but the basic idea will remain the same.

+11
source

Source: https://habr.com/ru/post/1238185/


All Articles