本文介绍了使用HTTP代理中间件在reactjs应用程序中无法识别setupProxy.js路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在我的应用程序中实现了HTTP代理中间件,我的setupProxy.js路径无法识别.以下是我的代码,如果我做错了任何事情,请通知我.
I am implementing http proxy middleware in my react app , my setupProxy.js path's are not recognising . Below is my code please let me know if i am doing anything wrong.
应用程序组件
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
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)
当我启动应用程序时,有如下所示的日志,但没有替换目标
When i start the application there were logs shown like below but not replacing the target
推荐答案
通过您的代码,我可以看到您使用过create-react-app.您尚未显示package.json.您是否向其中添加了代理?我想您可能已经错过了.
With your code I can see that you have used create-react-app. You haven't shown your package.json. Did you add proxy to it? I think you might have missed it.
package.json
package.json
{
....
"proxy": "http://localhost:5000",
}
这篇关于使用HTTP代理中间件在reactjs应用程序中无法识别setupProxy.js路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!