Created
January 27, 2015 18:39
-
-
Save Kikketer/4e9e1a133de526ca9a80 to your computer and use it in GitHub Desktop.
Inject environment variables when returning the index file
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var environment = { | |
env: process.env.NODE_ENV || 'local' | |
}; | |
var envRegex = /\/\/node::environment\s/; | |
var envString = 'environment = ' + JSON.stringify(environment) + '; //'; | |
var injectEnvironment = function injectEnvironment(req, res) { | |
fs.readFile(path.join(__dirname, '/app/index.html'), function(err, file) { | |
res.send(file.toString().replace(envRegex, envString)); | |
}); | |
}; | |
app.get('/index.html', injectEnvironment); | |
// Use the following in your index.html (inside <script> tags) file to inject: | |
// node::environment | |
// it will place the environment object there, obviously you would want to Angular-ize it instead of being a global variable. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment