score:3
from the docs: https://create-react-app.dev/docs/proxying-api-requests-in-development/
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.
as an alternative, you can use environment variables instead: https://create-react-app.dev/docs/adding-custom-environment-variables/
you should create .env.development
and .env.production
files at the root of your project:
# .env.development
react_app_proxy=http://localhost:9090
# .env.production
react_app_proxy=<some other domain>
and consume it on the react app:
// anywhere in the react app
const react_app_proxy = process.env.react_app_proxy
score:0
the proxy
value in package.json
is static and there is no possible way to retrieve its value from the environment variables. so just use the dynamic way for the proxy registration like described here: https://create-react-app.dev/docs/proxying-api-requests-in-development/#configuring-the-proxy-manually
it will allow you to retrieve any environment variable with the syntax process.env.some_name
example:
const { createproxymiddleware } = require('http-proxy-middleware')
module.exports = function(app) {
app.use(createproxymiddleware('/api', {
target: process.env.backend_url,
changeorigin: true,
})
);
};
now i have answered your primary question, i think this is an xy problem. when running a react application on a remote environment, there should be a reverse proxy (e.g: nginx, apache, ...) in charge of redirecting frontend requests (assets and routes) on the statically served react files, and the backend requests on the backend. the rule to implement is the following:
- if the incoming requests corresponds to a predefined pattern (e.g:
/api/...
), forward to the internal url of the backend - else, if the request corresponds to a file from react's build, serve it
- else, assert it's a route, then serve
index.html
the react's web server should be used on development machines only. it provides real-time compilation and live reload, which is of no use on a production environment, and it's probably not tested against security vulnerabilities. it's still not a bad idea to load the backend's url from environment variables, as it allows your code base to remain the same for every developer working on your project.
Source: stackoverflow.com
Related Query
- How do I configure my package.json file to read from an env var set in my OS?
- how to read data from json file an plot it in kepler.gl using React
- How to set elements from a json file into a React component
- How to read json file from my src folder in my react app using webpack-dev-server?
- How to read object URI (JSON file) from JSON file
- How to get data from local json file using actions and axios.get() (Redux)?
- How to open a text file and read the contents from it on react native?
- How do I return a json file from s3 to a specific url, but only that url
- How to get Image Url from local Json file in React.js
- How to loop over JSON data from external file in ReactJS?
- How to fetch and display data from json file in react typscript
- How to read and write to local JSON files from React.js?
- How to read from a csv file in a React app?
- How to import data from a JSON file in React?
- How do you set types on getting Object.keys on JSON file in TypeScript?
- How to load images in React-Table from JSON file
- In Reactjs, how can I return images from a json file where I already put image links?
- How to read xml from file inside public directory in react project in React JS?
- read JSON file from local hard drive and parse into react app
- How to send file + JSON in single request from react to nodejs
- How to generate components with each value from a JSON file in React?
- How to get nested Object from JSON File using React JS
- How to replicate effect of NODE_PATH env var with jsconfig file
- How to update a function with useEffect (like componentDidUpdate) and use .map function to display from JSON file ReactJS
- How to create a list of option tags from a JSON file in ReactJS
- How do I display data from my json file that match my filters
- How can i read a single element from a Json object in react?
- webapi, how to read the file from a POST/PUT action with custom model binder
- How I can define image in React component if path to image I get from JSON file
- How can I load the array images from JSON file in react gatsby?
More Query from same tag
- React.useContext is undefined
- Triggering another module export value (ReactJS)
- How to use Action Creators in Redux-form
- Focus On Input With Error on Form Submission
- Property 'data' does not exist on type 'AxiosResponse<any> | undefined'
- Setting state on componentDidMount()
- How to avoid destructuring mutation from Apollo useMutation hook?
- React Jest test didn't recognize import Alias
- Design Pattern Help: How to render dynamic views from a single controller component?
- React.js : Rendering the same component differently based on the id passed
- React - Redux - Dispatch Action Error, this.props.mostrarProduto is not a function
- Trouble with font awesome and react mao
- Recompose withProps
- Input value resets when input is cleared
- React - How to pass props down for the .map function when using functional components
- Wrapping functional JSX components with other functions
- Enzyme: how to test a key down?
- npm ERR! missing script: build in REACT DEPLOYED IN HEROKU
- Display the preselected data in the dropdownlist
- Cannot access object data in Gatsby page query
- How do I position an element in middle of a page and then make it sticky and go to top?
- Getting one data at table row
- Typescript React: How to declare types for specific element which is Props
- Why is "this" bound different in some instances, but the same in others in React?
- How can I safely set new state of a component when props change with useEffect (for an edit form modal component)?
- Add autosuggest to TagsInput
- Ref issue using react-hook-form
- Zeit builds failing - Error: No output directory named "build" found
- Unable to implement image transition in react with styled-components
- making an image within a card fill in the space