Express Server Responds to POST Request Before Recieving the Body - node.js

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?

Related

Unexpected delay in API response time on Node server

I am working on a chat bot which uses node as its backend server. So, to respond back to user's query, I have to make some other api call which are taking long to respond which causes delay in providing response to the user.
My problem is whenever i call a third-party api from node server it's response time is around 500-900ms whereas Postman gives back the same response in 80-120ms.
I have tried using following modules, but all of them give almost same response time:
request
axios
https
How to achieve the same response time which i am getting in the Postman?

Axios and Express: Send a request to Express and get responses in multiple steps

I'm working on a React/Node program. Somewhere in the program, I want to send a request to back-end using Axios, then while the back-end function is processing the request, I want to get step by step responses to update the front-end by showing some notifications.
I could do this by sending multiple requests and waiting for each response. But the problem is that the first process in each step is identically the same in all steps and it will create some performance issues.
My question is:
Is there any way to send a single request to API, then on the back-end side, return the response in multiple steps while it's processing? Meanwhile on the front-end, get the updates from back-end and update the notifications on it?
Thank you very much
Sorry bro, I'm afraid that you can't do this with using HTTP alone since the connection is ended with a single response for a single request. You need to do this with multiple HTTP call with Axios.
Otherwise, you could use WebSocket.
Their are cool Module socket.io with planty examples and documentations.
check this out,
https://www.npmjs.com/package/socket.io

Why can't I use res.json() twice in one post request?

I've got an chatbot app where I want to send one message e.g. res.json("Hello") from express, then another message later e.g. res.json("How are you doing"), but want to process some code between the two.
My code seems to have some problems with this, because when I delete the first res.json() then the second one works fine and doesn't cause any problems.
Looking in my heroku logs, I get lots of gobbledy gook response from the server, with an IncomingMessage = {}, containing readableState and Server objects when I include both of these res.json() functions.
Any help would be much appreciated.
HTTP is request/response. Client sends a request, server sends ONE response. Your first res.json() is your ONE response. You can't send another response to that same request. If it's just a matter of collecting all the data before sending the one response, you can rethink your code to collect all the data before sending the one response.
But, what you appear to be looking for is "server push" where the server can send data to the client continually whenever it wants to. The usual solution for that is a webSocket connection (or socket.io which is built on top of webSocket and adds more features).
In the webSocket/socket.io architecture, the client makes a connection the server and the connection is kept open indefinitely. Then either side of the connection can send messages to the other end. This is most useful when the server wants to "push" data to the client at any time. In this case, the client establishes the connection, then the server can send data to the client over that connection at any time. The client registers a listener for incoming messages and will be notified anytime the server sends it some data.
Both webSocket and socket.io are fully supported in modern browsers and in node.js. I would personally recommend using socket.io because some of the features it adds (a messaging layer, auto-reconnect, etc...) are very useful.
To use a continuously connected socket like this, you will have to make sure your hosting infrastructure is properly configured to allow it.
res.json() always sends the response to the client immediately (calling it again will cause an error). If you need to gradually build up a response then you can progressively decorate a plain old javascript object; for example, appending items to an array. When you are done call res.json() with the constructed response.
But you should post your code so we can see what's happening.

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.

how to tell express.js to queue the requests in a certain order

I am sending requests to a route using express.js
my problem is that i am getting the requests according to the order in which they came to express.
imagine i have a create request which takes very long but i dont care to postpond it, and a get request (to the same route) that takes a very short while.
now imagine i send 3000 create requests (without waiting to responses) and then 1 get request. express is giving me the get request into the server's api only after sending in all the 3000 creations. i want to tell express "hey you, listen, if you got a request which has "get" in its body, place it in the head of the queue and not its tail". this way, express will give me the get request as the next request even that there were 2999 create request comming before it that weren't handled yet.
I tried to do a work queue in the server itself but it didn't solve the issue because the "get" job didn't reach my api at all until I handled many many create requests.
can i do that? if so, how?
Thanks for any help.

Resources