0

Hello I have this question I have not found an anwser yet. I would like to create something like the image I attach. Final Structure I want to realize

The normal workflow should be something like this:

  1. Client sends a request to server 1;
  2. Server 1 receives the request;
  3. Server 1 sends a 302 (redirect) response to the client;
  4. Then client sends a new http request to server 2;
  5. Server 2 receives the request;
  6. Server 2 sends a 200 (ok) response to the client;

Instead of this I would like to realize something like:

  1. Client sends a request to server 1;
  2. Server 1 receives the request;
  3. Server 1 forward the request to server 2;
  4. Server 2 receives the request;
  5. Server 2 sends a direct response to the client;

So I do not want to send a redirect response from server 1 to client and then a new request from client to server 2!

It could be possible to realize something like this with node.js or, at least, with python?. I want this kind of architecture to speed up my service because server-server connection is far much faster then client-server connection.

Giuseppe
  • 307
  • 3
  • 18

2 Answers2

1

If you need to do some logic before server1 perform the request to server2 (for example selecting the endpoint in server 2 based on some condition on request) then you can use request function from the Node.js standard library http module (see docs at https://nodejs.org/dist/latest-v4.x/docs/api/http.html#http_http_request_options_callback). But if you don't need to perform any logic then is better to go with https://www.npmjs.com/package/http-proxy because you avoid a lot of boilerplate code.

yeiniel
  • 2,416
  • 15
  • 31
  • Yes I need to do some logic. Firstly server 1 checks if a file is present within filesystem - if file is present it is returned back to the client - otherwise server 2 is invoked to fetch the file. So I suppose the first possibility you mentioned is the right one. Then omit implementations I really want to know if on the way back to the client it is necessary to go through server 1 from server 2 (it would be an enormous waste of time) – Giuseppe Feb 13 '16 at 09:07
  • Once server1 perform the HTTP request to server2 then you can pipe the server2 response into the server1 response. This way server2 response is streamed by server1 to the client. Direct connection from the client to server2 is not strictly possible because the request to server2 is performed by server1 and not the client. – yeiniel Feb 14 '16 at 14:58
0

You need to use a proxy.

Something like https://www.npmjs.com/package/http-proxy

or you can use nginx or similar in front of a node.js server to be the proxy.

The only thing is the requests have to go back through the proxy, because thats how tcp connections work

akc42
  • 4,893
  • 5
  • 41
  • 60
  • There is no way to communicate directly from server 2 to the client? Going through server 1 would be a waste of time (because server 1 can't elaborate other requests) – Giuseppe Feb 13 '16 at 10:07
  • If you program server 1 properly - yes it can elaborate other requests. The connection from the client is to server 1, and the request and response have to go down the same connection. Web servers are designed to support multiple connections at the same time, so you are not blocking other requests by then passing the request through to server 2. Proxying like this is standard practice for big web sites where the bottleneck is server 2 not server 1. – akc42 Feb 13 '16 at 10:59