如何将请求转发到 node.js 中的其他端点

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32140987/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 19:15:36  来源:igfitidea点击:

How to forward a request to other endpoint in node.js

node.js

提问by mjimcua

In my scenario I need forward get request to another end point. In my machine there are two servers php and node.js server. Node.js is like a "man in the middle", PHP server must work in the same way.

在我的场景中,我需要将 get 请求转发到另一个端点。在我的机器上有两台服务器 php 和 node.js 服务器。Node.js 就像一个“中间人”,PHP 服务器必须以同样的方式工作。

Node.js server code

Node.js 服务器代码

var express = require('express');
var fs = require('fs');
var path = require('path');
var http = require('http');
var https = require('https');
var app = express();

var HTTP_PORT = 3000;

// Create an HTTP service
http.createServer(app).listen(HTTP_PORT,function() {
  console.log('Listening HTTP on port ' + HTTP_PORT);
});


//endpoint for tracking
app.get('/track', function(req, res) {

  sendRequestToOtherEndPoint(req);

  processRequest(req);
  res.setHeader('Content-Type', 'application/json');
  res.send('Req OK');
});

function processRequest(req){
    console.log("request processed");
}

function sendRequestToOtherEndPoint(req){
    //magic here :)
}

When this server receive a get request in port 3000, it process request information and it must forward the same requesto to another end point.

当这个服务器在 3000 端口收到一个 get 请求时,它会处理请求信息,并且它必须将相同的请求转发到另一个端点。

For example:

例如:

  1. Get localhost:3000/track?param1=1&param2=2
  2. Server process get request
  3. Server forward get request to localhost/final-endpoint?param1=1&param2=2
  1. 获取 localhost:3000/track?param1=1¶m2=2
  2. 服务器进程获取请求
  3. 服务器转发获取请求到 localhost/final-endpoint?param1=1¶m2=2

采纳答案by kmandov

Depending on what you're trying to do, you can create a new request to the end-point:

根据您尝试执行的操作,您可以向端点创建一个新请求:

//endpoint for tracking
app.get('/track', function(req, res) {

  req.get({url: 'http://end-point', headers: req.headers});

  processRequest(req);
  res.setHeader('Content-Type', 'application/json');
  res.send('Req OK');
});

More info: https://github.com/request/request

更多信息:https: //github.com/request/request

回答by sohel khalifa

In you case res.redirectmight help.

在你的情况下res.redirect可能会有所帮助。

app.get('/track', function(req, res) {
   // process the request

   // then redirect
   res.redirect('/final-endpoint');
});

Then catch the redirected request in final endpont.

然后在最终端点中捕获重定向的请求。

app.get('/final-endpoint', function(req, res) {
       // proceess redirected request here.
});

See the Express docs

查看Express 文档

回答by cjol

If your second endpoint is on a different server, (e.g. PHP) then you're going to need to either redirect the client (as in sohel's answer), or spoof a request from Node to the PHP server and then send the response back to the client. This latter option is definitely non-trivial so I would question whether it's crucial not to use a client redirect.

如果您的第二个端点在不同的服务器上(例如 PHP),那么您将需要重定向客户端(如sohel 的回答),或者将请求从 Node 欺骗到 PHP 服务器,然后将响应发送回客户端。后一个选项绝对不是微不足道的,所以我会质疑不使用客户端重定向是否至关重要。

If you're talking about two express endpoints, then I think the simplest answer might be not to actually forward at all, but just use the endpoint callback directly instead:

如果您在谈论两个快速端点,那么我认为最简单的答案可能是根本不实际转发,而是直接使用端点回调:

app.get('/track', trackCallback);
app.get('/otherendpoint', otherendpointCallback);

function otherendpointCallback(req, res) {
  // do your thing
}

function trackCallback(req, res) {

  otherendpointCallback(req, res);

  processRequest(req);
  res.setHeader('Content-Type', 'application/json');
  res.send('Req OK');
};

Depending on exactly what you want to do at the other end point, you might need to spoof some of req's fields (e.g. req.url)

根据您想要在另一个端点执行的确切操作,您可能需要欺骗req's 的某些字段(例如req.url