【问题标题】:How does node-http-proxy parse the target url?node-http-proxy如何解析目标url?
【发布时间】:2018-08-10 20:27:17
【问题描述】:

我遇到了一个问题,我觉得node-http-proxy 正在更改我的目标链接。下面我举了几个例子。

我使用 express 作为我的服务器并使用 Metaweather API。

问题是我能够从下面的端点获取数据 https://www.metaweather.com/api/location/2487956/ https://www.metaweather.com/api/location/2487956/2013/4/30/

但是当我尝试从https://www.metaweather.com/api/location/search/?lattlong=36.96,-122.02调用API时

它以状态码 500 失败,我认为 node-http-proxy122.02 之后添加了一些值,因为它没有被 / 关闭

server.js

const express = require("express");
const next = require("next");
const dev = process.env.NODE_ENV !== "production";
const app = next({ dev });
const handle = app.getRequestHandler();

const httpProxy = require("http-proxy");

const proxyOptions = {
  changeOrigin: true
};

const apiProxy = httpProxy.createProxyServer(proxyOptions);

const apiUrl =
  "https://www.metaweather.com/api/location/search/?lattlong=36.96,-122.02";

/*
https://www.metaweather.com/api/location/search/?lattlong=36.96,-122.02 - failed with 500
https://www.metaweather.com/api/location/2487956/ - passed
https://www.metaweather.com/api/location/2487956/2013/4/30/ - passed
*/

app
  .prepare()
  .then(() => {
    const server = express();

    server.use("/api", (req, res) => {
      console.log("Going to call this API " + apiUrl);
      apiProxy.web(req, res, { target: apiUrl });
    });

    server.get("*", (req, res) => {
      return handle(req, res);
    });

    server.listen(3000, err => {
      if (err) throw err;
      console.log("> Ready on http://localhost:3000");
    });
  })
  .catch(ex => {
    console.error(ex.stack);
    process.exit(1);
  });

感谢您调查此问题。

【问题讨论】:

    标签: javascript node.js express http-proxy node-http-proxy


    【解决方案1】:

    我已经在 node-http-proxy 中重现了这种情况。

    common.js 中有一个名为 urlJoin 的函数,它将 req.url 附加到目标 url 的末尾。

    我不完全确定意图是什么,但这是一个开始。

    这是我的测试:

    const urlJoin = function() {
      //
      // We do not want to mess with the query string. All we want to touch is the path.
      //
    var args = Array.prototype.slice.call(arguments),
        lastIndex = args.length - 1,
        last = args[lastIndex],
        lastSegs = last.split('?'),
        retSegs;
    
    args[lastIndex] = lastSegs.shift();
    
    //
    // Join all strings, but remove empty strings so we don't get extra slashes from
    // joining e.g. ['', 'am']
    //
    retSegs = [
      args.filter(Boolean).join('/')
          .replace(/\/+/g, '/')
          .replace('http:/', 'http://')
          .replace('https:/', 'https://')
    ];
    
    // Only join the query string if it exists so we don't have trailing a '?'
    // on every request
    
    // Handle case where there could be multiple ? in the URL.
    retSegs.push.apply(retSegs, lastSegs);
    
    return retSegs.join('?')
    };
    
    let path = urlJoin('/api/location/search/?lattlong=36.96,-122.02', '/');
    
    console.log(path);
    //                 /api/location/search/?lattlong=36.96,-122.02/
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-13
      • 2023-03-12
      • 1970-01-01
      • 1970-01-01
      • 2021-03-20
      相关资源
      最近更新 更多