【问题标题】:Proxy with nodejs使用 nodejs 代理
【发布时间】:2011-09-25 11:54:18
【问题描述】:

我针对 api 开发了一个 webapp。由于 api 没有在我的本地系统上运行,我需要代理请求,所以我不会在跨域问题中运行。有没有一种简单的方法可以做到这一点,所以我的 index.html 将从本地和所有其他 GET、POST、PUT、DELETE 请求发送到 xyz.net/apiEndPoint。

编辑:

这是我的第一个解决方案,但没有奏效

var express = require('express'),
    app = express.createServer(),
    httpProxy = require('http-proxy');


app.use(express.bodyParser());
app.listen(process.env.PORT || 1235);

var proxy = new httpProxy.RoutingProxy();

app.get('/', function(req, res) {
    res.sendfile(__dirname + '/index.html');
});
app.get('/js/*', function(req, res) {
    res.sendfile(__dirname + req.url);
});
app.get('/css/*', function(req, res) {
    res.sendfile(__dirname + req.url);
});

app.all('/*', function(req, res) {
    proxy.proxyRequest(req, res, {
        host: 'http://apiUrl',
        port: 80
    });

});

它将提供索引、js、css 文件,但不会将其余文件路由到外部 api。这是我收到的错误消息:

An error has occurred: {"stack":"Error: ENOTFOUND, Domain name not found\n    at IOWatcher.callback (dns.js:74:15)","message":"ENOTFOUND, Domain name not found","errno":4,"code":"ENOTFOUND"}

【问题讨论】:

    标签: node.js proxy express


    【解决方案1】:

    看看readme for http-proxy。它有一个如何调用proxyRequest的例子:

    proxy.proxyRequest(req, res, {
      host: 'localhost',
      port: 9000
    });
    

    根据错误消息,听起来您将虚假域名传递给 proxyRequest 方法。

    【讨论】:

    • 你说得对,主机以'http://'开头。删除这个修复它。但知道我有一个空响应:HTTP/1.1 400 Bad Request X-Powered-By: Express server: Apache-Coyote/1.1 content-length: 0 date: Sun, 25 Sep 2011 18:06:22 GMT 连接:关闭
    • @AndreasKöberle,对于初学者,我建议编辑这个问题,或者打开一个新问题。
    • 好的,我问了一个新问题。 stackoverflow.com/questions/7559862/…
    • 什么是 Node.js 的代理,为什么有人需要它?
    【解决方案2】:

    其他答案有些过时了。

    这里是如何将 http-proxy 1.0 与 express 一起使用:

    var httpProxy = require('http-proxy');
    
    var apiProxy = httpProxy.createProxyServer();
    
    app.get("/api/*", function(req, res){ 
      apiProxy.web(req, res, { target: 'http://google.com:80' });
    });
    

    【讨论】:

      【解决方案3】:

      这是一个可以修改请求/响应正文的代理示例。

      它评估实现透明读/写流的through 模块。

      var http = require('http');
      var through = require('through');
      
      http.createServer(function (clientRequest, clientResponse) {
      
          var departureProcessor = through(function write(requestData){
      
              //log or modify requestData here
              console.log("=======REQUEST FROM CLIENT TO SERVER CAPTURED========");
              console.log(requestData);
      
              this.queue(requestData);
          });
      
          var proxy = http.request({ hostname: "myServer.com", port: 80, path: clientRequest.url, method: 'GET'}, function (serverResponse) {
      
              var arrivalProcessor = through(function write(responseData){
      
                  //log or modify responseData here
                  console.log("======RESPONSE FROM SERVER TO CLIENT CAPTURED======");
                  console.log(responseData);
      
                  this.queue(responseData);
              });
      
              serverResponse.pipe(arrivalProcessor);
              arrivalProcessor.pipe(clientResponse);
          });
      
          clientRequest.pipe(departureProcessor, {end: true});
          departureProcessor.pipe(proxy, {end: true});
      
      }).listen(3333);
      

      【讨论】:

        【解决方案4】:

        也许你应该看看我的回答here。在这里,我使用请求包将每个请求通过管道传输到代理,并将响应返回给请求者。

        【讨论】:

          猜你喜欢
          • 2018-03-09
          • 2014-01-20
          • 2018-12-28
          • 2018-08-27
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-05-26
          • 1970-01-01
          相关资源
          最近更新 更多