I am new to UI/react and got work in my project to configure .env file. Since I have limited time, I tried to go through read.md of react where section for Adding Custom Environment Variables is mentioned. It was mentioned there The environment variables are embedded during the build time. Hence I made changes as per link so that build is not required and values could be read at runtime.
Inside action.js file of react project, I am trying to make post request as below. Console.log is printing the proper value but inside post request, URL is not getting replaced.
export function createIdea(idea) {
return function (dispatch){
return axios.post('http://${process.env.REACT_APP_HOSTNAME}:${process.env.REACT_APP_PORT}/users/user',JSON.stringify(user),config).then(response => {
// dispatch
console.log('success')
dispatch({
type: CREATE_SUCCESSFUL,
payload:response.data
})
}).catch(response=>{
// dispatch
console.log(process.env.REACT_APP_HOSTNAME);
console.log(response)
dispatch({
type: CREATE_FAILED,
payload:response
})
})
}
}
Inside webpack.config.dev.js, make changes to read configuration at runtime. Took reference from this stackoverflow link Changes are as below in webpack.config.dev.js: At the top of the file , added
var dotenv = require('dotenv').config({path: '../.env'});
Inside plugins section, added
new webpack.DefinePlugin({
"process.env": dotenv.parsed
}),
My .env file kept at root location looks as below:
REACT_APP_HOSTNAME=localhost
REACT_APP_PORT=8080
please provide some pointers if any configuration is missed or anything is misunderstood by me or something else need to be done wrt axios