Unexpected delay in API response time on Node server - node.js

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?

Related

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

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?

Pre-flight request takes more time (275% more) than normal request

Overview
I'm using Angular 6 as a front end for an web application which will communicate with the REST API developed in NodeJs. I've an Issue that the Preflight request takes long time than the normal request.
Detail
My frontend Angular 6 application communicates with the REST API to get data from the Database and display it to the user. I'm sending the request via HttpClient to the REST API.
In my REST API developed in NodeJs all CORS configurations are done correctly and the preflight request are successfully sent and the actual request are processed perfectly in local development machine.
The issue what I've is that, when I deploy the application in the production machine the options (preflight) request takes more time than the actual GET / POST request (See Image attached). As you can see the Actual GET request takes only 239ms while the preflight (OPTIONS) request takes 656ms, which is almost 275% more than normal. This happens in all HTTP request which in turn affects my website performance.

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

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