score:10

Accepted answer

to tell the development server to proxy any unknown requests to your api server in development, add a proxy field to your package.json, for example:

"proxy": "http://localhost:4000",

this way, when you fetch('/api/todos') in development, the development server will recognize that it’s not a static asset, and will proxy your request to http://localhost:4000/api/todos as a fallback. the development server will only attempt to send requests without text/html in its accept header to the proxy.

"keep in mind that proxy only has effect in development (with npm start), and it is up to you to ensure that urls like /api/todos point to the right thing in production."

note: this feature is available with react-scripts@0.2.3 and higher.

more details here: https://github.com/facebook/create-react-app/blob/master/packages/react-scripts/template/readme.md#proxying-api-requests-in-development


Related Query

More Query from same tag