setupProxy.js path are not recognizing in reactjs application using http proxy middleware

依然范特西╮ 提交于 2021-01-07 02:54:53

问题


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.

App component

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 }));
}

output

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


回答1:


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

{
   ....
   "proxy": "http://localhost:5000",
 }


来源:https://stackoverflow.com/questions/64639757/setupproxy-js-path-are-not-recognizing-in-reactjs-application-using-http-proxy-m

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!