【问题标题】:How to proxy my API URL to some other proxy如何将我的 API URL 代理到其他代理
【发布时间】:2019-07-29 07:04:40
【问题描述】:

我在http://localhost:8081/post URL 上,我的API 托管在某个“http://dummy.restapiexample.com/api/v1”上。

我希望它出现在“/custom/create”之类的网络选项卡中。但网络选项卡显示“http://localhost:8081/custom/create”,但不应在其中附加 localhost URL。它应该采用托管的 API URL。

我正在使用 CRA 样板。而且我不想用 express 来做同样的事情。

我尝试了以下代码

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

module.exports = function(app) {
    app.use(proxy("/custom",
    {target: "http://dummy.restapiexample.com/api/v1"}));
};

对于 API 调用,我使用以下格式:

fetch('/custom/create', {
    method: 'POST',
    headers: { 'Content-Type': 'application/json' },
    body: JSON.stringify(authData),
})

我不知道如何隐藏整个托管 API URL 和显示代理 URL 在网络标签中。

请就此提出宝贵的建议。

我是代理中间件的新手。提前谢谢你:)

【问题讨论】:

    标签: reactjs http-proxy-middleware


    【解决方案1】:

    在你的 package.json 中包含你的 API 的 url 作为代理。

      "proxy": "http://dummy.restapiexample.com",
    

    【讨论】:

    • 如果我有多个 API 托管 url 呢?无论如何,您的解决方案对我不起作用。任何其他想法@Muljayan
    • 如果你使用像 axios 这样的包,你可以设置基本 URL 并拥有多个 API 实例
    • 您的想法与我的问题不符。
    猜你喜欢
    • 2018-01-17
    • 2021-04-10
    • 2018-09-12
    • 2021-12-22
    • 1970-01-01
    • 1970-01-01
    • 2013-02-10
    • 1970-01-01
    • 2021-02-28
    相关资源
    最近更新 更多