简体   繁体   English

我可以将http请求从一个node.js或python服务器转发到另一台服务器吗?

[英]Can I forward an http request from one node.js or python server to another server?

Hello I have this question I have not found an anwser yet. 您好,我有这个问题,我还没有找到答案。 I would like to create something like the image I attach. 我想创建类似我附加的图像的东西。 我要实现的最终结构

The normal workflow should be something like this: 正常的工作流程应如下所示:

  1. Client sends a request to server 1; 客户端向服务器1发送请求;
  2. Server 1 receives the request; 服务器1收到请求;
  3. Server 1 sends a 302 (redirect) response to the client; 服务器1向客户端发送302(重定向)响应;
  4. Then client sends a new http request to server 2; 然后,客户端向服务器2发送新的http请求;
  5. Server 2 receives the request; 服务器2收到请求;
  6. Server 2 sends a 200 (ok) response to the client; 服务器2向客户端发送200(确定)响应;

Instead of this I would like to realize something like: 除此以外,我想实现以下内容:

  1. Client sends a request to server 1; 客户端向服务器1发送请求;
  2. Server 1 receives the request; 服务器1收到请求;
  3. Server 1 forward the request to server 2; 服务器1将请求转发到服务器2;
  4. Server 2 receives the request; 服务器2收到请求;
  5. Server 2 sends a direct response to the client; 服务器2向客户端发送直接响应;

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! 因此,我不想将重定向响应从服务器1发送到客户端,然后将新请求从客户端发送到服务器2!

It could be possible to realize something like this with node.js or, at least, with python?. 使用node.js或至少使用python?可以实现类似的功能。 I want this kind of architecture to speed up my service because server-server connection is far much faster then client-server connection. 我希望这种架构可以加快我的服务速度,因为服务器与服务器的连接要比客户端与服务器的连接快得多。

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) . 如果需要在server1执行对server2的请求之前做一些逻辑(例如,根据请求的某些条件在服务器2中选择端点),则可以使用Node.js标准库http模块中的请求功能(请参阅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. 但是,如果您不需要执行任何逻辑,那么最好使用https://www.npmjs.com/package/http-proxy,因为这样可以避免很多样板代码。

You need to use a proxy. 您需要使用代理。

Something like https://www.npmjs.com/package/http-proxy 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. 或者您可以在node.js服务器之前使用nginx或类似服务器作为代理。

The only thing is the requests have to go back through the proxy, because thats how tcp connections work 唯一的事情是请求必须通过代理返回,因为那就是tcp连接的工作方式

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 http在Node.js和Python Flask服务器之间发布了base64 json请求,但是Node收到了不完整的json - http post a base64 json request between Node.js and Python Flask server but Node received an incomplete json 将自定义响应从 Node.js 服务器发送回 python 代码,用于 POST 请求 - Send a custom response back to python code from the Node.js server, for a POST request 本地node.js服务器和python简单http服务器之间有什么区别 - What's the differences between a local node.js server and a python simple http server python服务器和node.js webclient连接 - python server and node.js webclient connection 拦截请求,然后转发到另一台服务器 - Intercept request and then forward to another Server 为什么我的 Node.js gRPC 客户端需要 3 秒才能向我的 Python gRPC 服务器发送请求? - Why does my Node.js gRPC client take 3 seconds to send a request to my Python gRPC server? 无法将Node.js Thrift客户端连接到Python Thrift服务器 - Can't Connect Node.js Thrift Client to Python Thrift Server 通过Node JS服务器中的http请求接收二进制数据 - Receiving binary data via http request in node js server 通过 protobuf 在服务器(node.js)和客户端(python/node.js)之间交换消息 - exchanging messages between server(node.js) and client(python/node.js) via protobuf 在Node.js服务器上运行Python脚本时出错 - Error running Python script on Node.js server
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM