How to set environment variables in my client code?

I need to install a different key depending on whether we are in development or production. What is a good way to do this on a client side that does not have a built-in runtime?

Thank!

+5
source share
3 answers

One way is to insert the environment variable from the server into the global namespace on the client.

For example, if you did this in PHP:

<script>
    var env = <?php echo $your_env_variable; ?>; // globally accessible variable
</script>

Now you can access this environment variable from any javascript files that are executed after this script tag.

In node, you would do the same, but with a template (e.g. Jade):

script(type='text/javascript').
    var env = passedInEnvVar
+4
source

nodejs "" - "NODE_ENV". , HTML, expressjs .

( EJS)

  var key = '<%= keys[process.env.NODE_ENV] %>'

,

var keys = {
  'development': 'dev-key',
  'production': 'prod-key'
}
+1

script? script GET, . http://example.org/script.js?development=1, , PHP.

-, .

0

All Articles