Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Server not responding until the request data is processed #3155

Closed
fatfisz opened this issue Oct 2, 2015 · 8 comments
Closed

Server not responding until the request data is processed #3155

fatfisz opened this issue Oct 2, 2015 · 8 comments
Labels
http Issues or PRs related to the http subsystem. question Issues that look for answers.

Comments

@fatfisz
Copy link

fatfisz commented Oct 2, 2015

I've created a simple server:

var server = require('http').createServer(function (req, res) {
  res.write('ok');
  res.end();
});

server.listen(8080);

When I'm POST-ing some data (a lot of data) the server won't respond until all of the data has been processed. It is ignored obviously, because there is no data event listener set up.

The question is, is there a way to skip the wait and still provide a meaningful response?

There is a scenario in which this would be useful: I'd like to limit the data I receive, and respond with an appropriate error immediately after the limit has been reached.

I've read the docs, read some source files, and didn't find the answer. Is it a bug in Node, a limitation of the HTTP protocol, or did I miss something?

The Answer (edited)

So it turns out the browsers are at fault: http://serverfault.com/questions/360479/why-might-a-413-not-be-flushed-to-the-client-immediately. As @ChALkeR has written, the client has to handle the response, but XMLHttpRequest (which I was using) doesn't work like this. There is a chance that the fetch API allows reading the response before the full request is sent (with streams), but I'm not sure about that.

Another option would be to upload using a WebSocket connection. The client in the browser would be able to send a chunked request and listen for a response in the meanwhile. But this is only a workaround, because I want to be able do it with simple HTTP.

@brendanashworth brendanashworth added question Issues that look for answers. http Issues or PRs related to the http subsystem. labels Oct 2, 2015
@evanlucas
Copy link
Contributor

This would be better asked at https://github.com/nodejs/help or stack overflow. The server should be able to respond before reading all of the data. Are you sure the client is not staying alive to send the data though?

@fatfisz
Copy link
Author

fatfisz commented Feb 2, 2016

Yes, the client is staying alive, but I want to ignore it.

Thanks for the link - I'll surely ask there too, but I thought this was a nodejs issue.

@cjihrig
Copy link
Contributor

cjihrig commented Feb 2, 2016

If you ask on nodejs/help, SO, or somewhere else, and come to the conclusion that this is actually a bug in Node, please report back here. Closing for now.

@cjihrig cjihrig closed this as completed Feb 2, 2016
@fatfisz
Copy link
Author

fatfisz commented Feb 2, 2016

It's quite late to add that info, but I actually got redirected here from here: expressjs/express#2765

@dougwilson has written that "The issue here is a Node.js core issue", so I opened an issue here, believing that he has also researched the topic quite a bit.

@ChALkeR
Copy link
Member

ChALkeR commented Feb 2, 2016

Aborting http requests and getting a response before the request body is sent has to be supported on the client. There is an Expect header field specifically for that purpose, google for it, or ask at StackOverflow. I personally never used it, though, so I'm not aware how to work with it with Node.js.

@ChALkeR
Copy link
Member

ChALkeR commented Feb 2, 2016

In fact, it's there, of course: https://nodejs.org/api/http.html#http_event_checkcontinue

@fatfisz
Copy link
Author

fatfisz commented Feb 2, 2016

Thanks a lot, will definitely look into it then! I tried googling before ofc, but I guess sometimes you have to know the exact thing you're looking for...

Also it's nice that this question has gotten some attention after a few idle months 😄

@fatfisz
Copy link
Author

fatfisz commented Jun 4, 2016

I've found the answer and I've put it in the first comment for future reference.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
http Issues or PRs related to the http subsystem. question Issues that look for answers.
Projects
None yet
Development

No branches or pull requests

5 participants