NodeJS is server side code, and your click would come from the client. So you'd need some way of talking to the server from the client. One way you could do it would be to create a route in express that when posted to, changes the environment variable. It would look something like this:
app.post('/change', (req, res) => {
const newVar = req.body.global;
app.locals.language = newVar;
res.json({
success: true
});
});
So when you POST
to the route with a variable called 'global'
it will change your language to whatever was sent. From the client, you would do something like this:
<button onClick="changeGlobal('EN')">Change Global To EN</button>
<button onClick="changeGlobal('ES')">Change Global To ES</button>
function changeGlobal(language) {
fetch('/change', {
method: 'POST',
headers: {'Content-Type':'application/json'},
body: JSON.stringify({global: language})
})
.then(res=>res.json())
.then(res => console.log(res));
}
You should see a log out with an object of {success: true}