Express Intercept Response before sending it to client - node.js

I have one question.
In my current application I am using express, and I and using Express.get and Express.post method multiple times because I have multiple pages.
So, I want to intercept each response before sending it to client to add some state in response object.
Is there any way in express.js by which I can intercept each response before sending it to client.

Related

How to know a POST request was received

I have a structure where an application sends a POST request, the API handles the parameters and enters them into the database then another application that is supposed to run a function when the POST request was handled. The only issue I'm having is how can I make the 2nd application know when the request was handled other than running a GET request on a time interval?
Another explanation:
Client A: POSTs data
API: Handles data
Client B: GETs and displays data to user
How can I tell Client B when to do the GET request?
This is all in node.js and using express
What you need is to push data to clients when something happens server-side. This can be achieved by either server side events (which do exactly this), or websockets which create a bidirectional communication channel between server and client. Which one to choose? Check out this stack overflow post.

NestJS proxy + standard route handling

I use NestJS app with http-proxy-middleware to proxy incoming request to the backend server.
If incoming request matches the proxy config it will handle the request and send response directly to the client without invoking standard route handling mechanism of the NestJS app.
Is there any way to process this same request with standard route handling mechanism in NestJS?
As an example what I want to achieve is to send Websocket message, SSE event, or run background task using NestJS capabilities based on response received from backend service.
Solutions I think of so far are the following:
Create a custom proxy middleware where instead of response.send() it will be calling next(), but may be some other proxy-middleware solution out there that would allow this?
Create interceptor, it will allow me to make a backend call and modify response based on backed response and then call next(), not sure if this will work.

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

Architecture for multiple intervaled request with multiple async request in them

Imagine a situation where a user send a request to the server and then the server needs to address an outer API sending 10 request to that API, each request must be with a 1 second wait time between each of them. When each of this request is resolved and a response returned and parsed I need to send each of the response items to another outer API once each of the items is completely resolved and parsed a socket.io connection should emit the results back to the client so he won't have to wait for 10 seconds until all the requests are resolved.
How would you address this kind of situation?
Been trying to use the 'when' library, sending proxy request using 'request-promise' but still can't wrap my head around on how to construct this kind of thing.
Added a diagram:

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.

Resources