I'm writing a socket.io app, and I'm getting annoyed at having to chance a line in my frontend scripts.js file every time I push to heroku, from
var socket = io.connect('http://localhost');
to
var socket = io.connect('http://ersatz-elephant-1337.herokuapp.com/');
I know I can use process.env.VAR
to access those variables on the backend, but I'm wondering if there's any way to programmatically determine which to use in my frontend scripts? I'd rather leave the scripts as static aside from this, but maybe that's not possible?
EDIT: I'm not really looking for a solution on the backend... I'm looking for a way that my /public/scripts.js
file can connect to the right thing. I guess maybe the only way to do that is by specifying something special in server.js
for the frontend script file, rather than serving it statically, but if so, then that's the instruction I'm looking for, not how to actually access the env vars on the server. Sorry for any confusion!