I am using PKG, to make a executable .exe file for my Appolo GraphQL Server + React client bundled all into one exe file. It's working, and now there is requirement: app should read from somewhere an IP address and use it as constants for host, port, etc.
From server side I am using fs.readFileSync
- which is okay. I can read any text file as it's nodejs server side. And now the requirement is to pass that IP to the client files -> into the static folder (marked *), where I have constant.js with IP of client to connect inside (compiled static/js..). Server side files (NodeJs+React):
├───build
│ └───static
│ ├───css
│ ├───*js*
│ └───media
├───config
├───src
│ ├───core
│ ├───graphql
│ ├───guardServer
│ └───pg
└───SSL
Client React files tree:
├───.idea
├───.vs
│ └───React
│ └───v16
├───build
│ └───static
│ ├───css
│ ├───js
│ └───media
├───public
└───src
├───components
│ ├───core
│ │ ├───functions
│ │ └───modal
│ └───modules
│ ├───account
│ ├───adminPanel
│ │ └───pages
│ ├───archive
│ ├───hr-department
│ │ └───pages
│ ├───logs
│ ├───monitor
│ └───reports
├───config
└───images
So my actions are: npm run build
a clent. Then I taking the folder build and replacing a server side files: server/build folder. Then I'm doing on the server side - npm run build
and receiving an exe file. WHERE a server part (/server/index.js) of this exe can read a settings.txt, but not that part (client) which is inside /server/build/static/js...
My question is: is it possible by modifying webpacker somehow, and server environment variable will be accessible (and will be dynamic) from client side with command process.env.myvar
?
If not, if there are any tricks I can utilize in this case? Please note that PKG's fs.writeFileSync
is now allowed due to the filesystem shapshot restriction.
Code of server side:
let fileContent = fs.readFileSync("D:\\settings.txt", "utf8");
let getIp=JSON.parse(fileContent); //fine working, got IP
Client using Apollo Client and exactly for him I need to pass that IP from server side, any ideas please?
Update for Ajeet Shah. What I did:
On my server.js - var __SERVER_DATA__ = "192.168.0.111";
in index.html of client - <script>
window.SERVER_DATA = __SERVER_DATA__;
</script>
in index.js of client - console.log(window.SERVER_DATA);
Update:
I think I will end up for a while with the tiny http server, which will run on localhost:port. And from there I can freely read any settings. Thank you All for usefull answers.
I've chosen the answer with more options and there was advise about localStorage. Thank you all much for heads up. Still trying to implement offered methods.
Update.
I've finishied doing this. I've used an html variable window.SERVER_DATA = __SERVER_DATA__
as described here - https://create-react-app.dev/docs/title-and-meta-tags/#injecting-data-from-the-server-into-the-page. I will decide later on what to do with best answer, if nobody will post extra ones.