Skip to content

Instantly share code, notes, and snippets.

@Kikketer
Created January 27, 2015 18:39
Show Gist options
  • Save Kikketer/4e9e1a133de526ca9a80 to your computer and use it in GitHub Desktop.
Save Kikketer/4e9e1a133de526ca9a80 to your computer and use it in GitHub Desktop.
Inject environment variables when returning the index file
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