NestJS proxy + standard route handling - nestjs

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.

Related

Nestjs how to use http request and Websocket at the same time

I have a controller in Nestjs that handles an HTTP request. There is an IoT device that communicates with the server and sends the latest changes to the server with a post request. At the same time, there is a mobile application that should receive the update in realtime with Websockets.
I know that the HTTP request and Websockets are different concepts in Nestjs, but is there any way to emit an event whenever I receive the HTTP request?
Any idea would be highly appreciated.
There is indeed a proper solution for this. You need an Injectable that contains an RxJS Subject. Whenever your Controller receives a value via the POST request, it delegates the value to the injected service. The service then "instructs" the Subject to emit the value.
On the other side of the chain, inside your WebSocket Gateway #SubscribeMessage, you return an RxJS Observable - which is derived from the Subject - to the connected clients.
More on Subject and Asynchronous responses
Here is the implementation.

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 Intercept Response before sending it to client

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.

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.

Accessing inbound HTTP headers in meteor?

I'm working on an application that relies on data that the browser sends within the HTTP headers (and there's no way around this). This also happens to be my first time working with something node.js based, so it's very likely I'm completely missing something simple!
Basically what I want to be able to do is call a method on the server from the client, and in that method read the HTTP headers that the client sent.
Meteor doesn't yet provide a supported API for serving HTTP from your app. This is intentional: in the not-too-distant future, your app server is likely to not be just a single process directly serving end users, but an arbitrarily parallelizable service behind a proxy tier. So we'll need to provide a supported API for responded to HTTP requests (REST, eg) that continues to work in such a setting.
Are you sure it needs to be HTTP and that you can't just use a Meteor method?
If you really need to accept direct HTTP requests now, take a peek at how packages/accounts-oauth-helper/oauth_server.js uses __meteor_bootstrap__.app to hook into the Connect middleware framework. This will work for now, but we don't promise that Meteor will always be powered by Connect :)

Resources