Just a quick question: How do I initialize .env
variables and then send them to client-side using (probably) server-side? Like, if I have a variable called NAME="Khaleel"
, how do I display it in the browser? Can it be done through a post request?
First your .env variable should be like this NAME=Khaleel
and not with quotes: Wrong: NAME="Khaleel"
You can use the process.env
Like:
index.get('/getname', (req, res) => {
res.status(200).send(process.env.NAME);
});
But this is not the purpose of your .env variables.
This variables needs to be secret like your bot token or some key or password that only the owner should know.
If you need a global variable in your code and not give your secret .env ones you can do this
const name = 'this name';
index.get('/getname', (req, res) => {
res.status(200).send(name);
});
Last advice if you need to send dynamic data to the client side you need to set an API with a database. Or use a json file like a database and use something like templates (ejs for example)
Good luck!
No, actually, I was not planning to send anything confidential.
Can you ellaborate more on this?