admin管理员组

文章数量:1435859

I have tried multiple things

I added

"proxy": "http://localhost:5000/"

to package.json, and even

devServer: {
    historyApiFallback: true,
     proxy: {
     "/": "http://localhost:5000"
     }
  }

to webpack.config.dev.js

However, none of these seem to work. When I go to localhost:3000/age (one of my routes), the backend does not receive any request even though the React ponent is tied to it AND I have the proxy set in package.json

My express app listens on route 5000, and I am visiting it through my browser as localhost:3000/age and also have tried localhost:5000/age but don't work

I have tried multiple things

I added

"proxy": "http://localhost:5000/"

to package.json, and even

devServer: {
    historyApiFallback: true,
     proxy: {
     "/": "http://localhost:5000"
     }
  }

to webpack.config.dev.js

However, none of these seem to work. When I go to localhost:3000/age (one of my routes), the backend does not receive any request even though the React ponent is tied to it AND I have the proxy set in package.json

My express app listens on route 5000, and I am visiting it through my browser as localhost:3000/age and also have tried localhost:5000/age but don't work

Share Improve this question asked May 16, 2020 at 4:12 mg ntmg nt 1914 silver badges14 bronze badges 8
  • Are you sure localhost:5000/age available on express route.please check it directly on browser url – prasanth Commented May 16, 2020 at 4:28
  • @prasanth I have a react router which loads a ponent when the path is ‘/age’, but going to that route in my browser says CANNOT GET ‘/age’. I do not have a get request, but my React Router is supposed to load a ponent – mg nt Commented May 16, 2020 at 4:50
  • please set the route on express .Because you are request on express router.react router is client router. its not performed by the express js – prasanth Commented May 16, 2020 at 7:02
  • But if react has a proxy, then shouldn’t react router use the same route as express? I’ll try and use fetch requests in my React app to use express, instead of react router – mg nt Commented May 16, 2020 at 17:35
  • why type of request you did direct url call or a ajax request for the url localhost:3000/age ? – prasanth Commented May 17, 2020 at 3:42
 |  Show 3 more ments

2 Answers 2

Reset to default 0

Check this documentation

The development server will only attempt to send requests without text/html in its Accept header to the proxy.

If you access directly on browser. its set content type header as text/html .So its not working . Better use only for an ajax/fetch request .

issue

for development react have own server.if you need a react side route better try react-router. After the production build its working same as you expect

You need to set your proxy your backend's port. If your backend is listening on 5000 then it should be "proxy": "http://localhost:5000"

By default any http requests in react to a sites own domain use whatever port the app is running on. Since react is running on 5000 itll send all requests through that port. The proxy tells it to change this default behavior to the specified proxy.

本文标签: javascriptReact proxy does not work even though config is set properlyStack Overflow