My React front-end is unable to call my node-express backend server, the fullstack app is deployed in heroku
If your React application is served from your Node.JS application as you said, you could just use window.location
. window.location
is an object that stores statistics about the current page that the user is on, and you could use that to construct a URL and send the server a request, like so:
// This URL uses a template literal, which is a new feature of ES6.// All but Internet Explorer supports it.// This is using window.location.protocol, which is either `http:` or `https:`,// depending on the protocol that the page was loaded with. window.location.host// is the host that the page was loaded from, with the port number.const postUrl = `${window.location.protocol}//${window.location.host}/post`;// And then requesting with the URL.addData = () => { console.log(postUrl); console.log(process.env, "AS"); Axios.post(postUrl, this.state.form) .then((response) => { this.setState({errorMessage: "",successMessage: response.data}); }) .catch((err) => { this.setState({successMessage: "",errorMessage: err.response.data.message}); });}