【发布时间】: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