Why Chrome Refreshes the URL after certain minutes of receiving no response? - node.js

In my node application I have an anchor tag which when clicked will request an Express GET route which makes some API calls and render the response in an EJS template.
When the API being requested from Express route takes too long to respond, the Node express route URL gets requested again automatically.
Can anyone explain this behaviour ?

In HTTP/1.1 Connections:
8.2.4 Client Behavior if Server Prematurely Closes Connection:
If an HTTP/1.1 client sends a request which includes a request body,
but which does not include an Expect request-header field with the
"100-continue" expectation, and if the client is not directly
connected to an HTTP/1.1 origin server, and if the client sees the
connection close before receiving any status from the server, the
client SHOULD retry the request
Browsers mostly will re-try a connecton until a proper response is given, on Chrome i believe it's 5 attempts.
It's a expected behavior.
There's a nice article from Oracle's blog that has a similar scenario described.

Related

Send updates about the state of a running HTTP request to the client

I would like to send updates of a running HTTP request to the client to tell it at what stage the request-triggered process currently is.
The process behind the request does currently the following things (in this order):
Client-side:
Client sends an HTTP Request (upload of a file) to the server
Server-side:
Takes the uploaded file
Encrypt it
Upload it to an archive storage
Return response to the client
(Meanwhile, the client does not know what currently happens)
Client-side:
Get response and show it to the user
I want to tell the client at what stage the process is, like “Uploading done. Encrypting…” and so on.
Is there a way to realize that, or am I missing something? Is it even possible to do?
Frameworks I'm using:
Client: Next.js
Server: Hapi.dev for API development
Thanks
You can send non-final 1xx header responses for your http request as described here.

HTTP response headers over clients

So I am little confused on the behaviour of HTTP headers. My question is:
If I am running an API off a server. If a client makes a request, and i respond to the client via the server by setting a header in the form of:
response.header.<some_header> = <some_value>
will this header persist for all clients (ie some computer, and then another computer, etc), or is this header response unique to the client making the request?
Thanks!!
The response object is only sent to the client that made a request in the first place. In fact you said the answer in your question yourself:
"If a client makes a request, and I respond to the client via the server"
In general if a client made a request, the server can only respond the the client that made the request. Hope this answers your question. For more information on http headers you should read this.

Express Server Responds to POST Request Before Recieving the Body

I have an express server that I created using express-generator, and setup by following one of the thousands of available online tutorials. As far as express servers go, it's fairly unremarkable. When I send POST request from Postman, or from my React app, it takes the body and stuffs it into the database, then sends the response, exactly as I expect.
The problem occurs when I try to send it a POST request from a microcontroller via a 4G modem using AT commands. This is considerably slower than a browser, and involves a delay of several milliseconds between the header and the body. During this delay, instead of waiting for the POST body, the server goes ahead and sends the response as though the body was empty.
At first, the problem appeared to be with the modem or the firmware, but I was able to narrow it down to the server by making POST requests to a different server. I made a POST to the dweet.io API, and observed that after the modem transmitted the header, it sat for a few seconds to allow the microcontroller to upload the body.
It feels like this has something to do with a timeout setting in express, but the only thing I could find in that department is server.timeout, which I have verified to be 120000. Is there any setting or middleware I could use to force the server to chill out and wait for the body?

How to do automatic API relay in express.js?

I am working on a frontend express.js app and need to request to a lot of apis from client side(browser) to another server. I need to request those apis from server side and send responses to browser, due to cross domain problem. I am now writing every api as a route method in my app to receive requests and re-send recieved data as responses. Because of the number of apis is huge, and rewrite every request is prone to error and hard to maintain, I wonder whether there is an express.js way to relay all requests with their methods and parameters not changed from browser to the other server. For example, if I request to some /api/test route of my server, it should request to /test route of the other server, receive response and respond it to me.
Unfortunately there is no way to change anything of the server which implemented the apis. So I could only do this work in the express.js app.
Thanks.

When handling XMLHttpRequest in Nodejs proxy server, I've already call "res.end()", but client's behavior seems can't get my res and timeout finally

I have a question.
In my Nodejs proxy server, from debug log, I can see I've already handled the request, send its correct response and call "res.end()" to end this request. And this request is XMLHttpRequest.
And from Chrome's developer tool, I can see that this request has got correct response from my Nodejs https server, and its status code is 200 OK.
But from the web UI, the client seems still waiting for my response because its image is always turning over and over, and seems to time out finally. Like the screenshot below.
Can anyone give me some advice? I don't understand why the client can't get my response data. And this request is XMLHttpRequest.Thanks very much.
I've found the answer. Because I need to response to long poll request immediately. If don't, the client will cancle the request or timeout

Resources