Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In client side javascript, how can change my variables based on environment?

Ok, so I am using node.js and it is awesome.

I have been working locally until very recently and I am a bit frustrated that I am changing my code each time just to deploy.

Specifically I am using socket io and I need to tell the socket where it lives:

var _socket = io.connect('http://localhost:5000');

When I have been going locally, this is great, but I need to change this each time I deploy to my test server...this is annoying. In other languages I can do this through a config file.

In node, on the server side, I can set variables based on my environment like so...

app.configure('development', function(){
    process.env.PORT = 5000;
});

Can I do something similar on the client side?

like image 765
David Avatar asked Dec 19 '25 01:12

David


2 Answers

You have to embed this variable to client-side HTML code somehow (i.e. in Jade templates).

If you show your server-side code serving HTML, I might be able to pinpoint where it might be.

like image 97
alex Avatar answered Dec 20 '25 14:12

alex


I usually do stuff like this:

(function(){
var config = {
    host: 'localhost',
    port: '8080',
    setEnv: function(env){
        switch (env){
            case 'development':
                this.host = 'devserver';
                this.port = '80';
                break;
            case 'production':
                this.host = 'prodserver';
                this.port = '80';
                break;
        }
    }
};

   console.log(config.host);
   console.log(config.port);
   config.setEnv('production');

   console.log(config.host);
  console.log(config.port);
})();
like image 26
shredmill Avatar answered Dec 20 '25 15:12

shredmill



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!