javascript - setupProxy.js 路径在使用 http 代理中间件的 reactjs 应用程序中无法识别
问题描述
我正在我的 react 应用程序中实现 http 代理中间件,我的 setupProxy.js 路径无法识别。以下是我的代码,如果我做错了什么,请告诉我。
应用组件
class App extends React.Component {
test = () => {
// I dont have any thing which is running in "/api"
// Simply called fetch with "/api" because setupPorxy.js is looking my request or not
fetch("/api")
.then(res => {
alert('test pri')
console.log('res', res)
})
}
render() {
return (
<div className="App">
<button onClick={this.test}>Test</button>
</div>
);
}
}
setupProxy.js
const { createProxyMiddleware } = require('http-proxy-middleware');
\\ I dont have any thing running on localhost 5000 , want to check the request is modified or not
module.exports = (app) => {
app.use(createProxyMiddleware('/api', { target: 'http://localhost:5000', changeOrigin: true }));
}
输出
But ended with 404 not found in browser console like http://localhost:3000/api (404 not found)
解决方案
通过您的代码,我可以看到您使用了 create-react-app。你还没有显示你的 package.json。你添加代理了吗?我想你可能错过了。
包.json
{
....
"proxy": "http://localhost:5000",
}
推荐阅读
- javascript - HTML5 - 有没有办法突出显示特定的一天?(想javascript)
- python - Discord Bots:检测另一个命令中的消息
- python - 用 pandas 刮硬币壁虎报告 HTTP 错误 403
- c# - 追加不添加到列表 C#
- javascript - JS appendChild 没有附加任何东西
- c# - 如何从 MongoDB MVC 列表中选择一个对象
- node.js - 使用 puppeteer 获取对象的更改
- visual-studio - 在 VS 2017/2019 中调试与受人尊敬的 .ascx 关联的 javascript 文件
- arm - arm-none-eabi 工具链中默认启动文件和链接器脚本的用途?
- python - 背景图像未出现在 Tkinter 中