├── .gitignore ├── README.md ├── client ├── app.js └── index.html ├── config.json ├── gulpfile.js ├── index.js └── package.json /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Sample project illustrating how to inject env varialbes into AngularJs app 2 | 3 | This app demonstrate a better way of injecting enviromental apps into angular using _gulp_ tasks. 4 | 5 | ## How to run 6 | 7 | Simply run `node index` to run node server. This will also generate angular configuration file and place it in client folder. 8 | Configuration file is angular constant service with key value pairs for certain node env variables. 9 | Process of injecting these values is done by gulp command: `config`. 10 | 11 | ## gulp config 12 | 13 | `gulp config` takes _config.json_ as default config and generates angular constant service. It will inject all the configs from _config.json_. If you want to override some of the value from the config or add new ones, add them to enviromental variables and `constants` in `configureSetup` (gulpfile.js). -------------------------------------------------------------------------------- /client/app.js: -------------------------------------------------------------------------------- 1 | (function(){ 2 | 'use strict'; 3 | 4 | var app = angular.module('myApp', []); 5 | 6 | app.controller('MainController', MainController); 7 | 8 | function MainController($scope, myNodeEnvVariable) { 9 | $scope.myNodeEnvVariable = myNodeEnvVariable; 10 | } 11 | 12 | })(); 13 | -------------------------------------------------------------------------------- /client/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |

MY_NODE_ENV_VARIABLE is: {{myNodeEnvVariable}}

10 | 11 | -------------------------------------------------------------------------------- /config.json: -------------------------------------------------------------------------------- 1 | { 2 | "myNodeEnvVariable": "foo" 3 | } 4 | -------------------------------------------------------------------------------- /gulpfile.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var gulp = require('gulp'); 4 | var gulpNgConfig = require('gulp-ng-config'); 5 | 6 | var configureSetup = { 7 | createModule: false, 8 | constants: { 9 | myNodeEnvVariable: process.env.MY_NODE_ENV_VARIABLE, 10 | } 11 | }; 12 | 13 | gulp.task('config', function() { 14 | gulp.src('config.json') 15 | .pipe(gulpNgConfig('myApp', configureSetup)) 16 | .pipe(gulp.dest('client')); 17 | }); -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var express = require('express'); 4 | var app = express(); 5 | 6 | var gulp = require('gulp'); // Load gulp 7 | require('./gulpfile'); // Loads our config task 8 | 9 | 10 | // Kick of gulp 'config' task, which generates angular const configuration 11 | gulp.start('config'); 12 | 13 | // serve client side files 14 | app.use(express.static('client')); 15 | 16 | app.listen(3000); 17 | 18 | console.log('Listening on port 3000'); -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "angular-config-vars", 3 | "version": "1.0.0", 4 | "description": "Example of how to correctly inject configuration into angular", 5 | "main": "index.js", 6 | "dependencies": { 7 | "express": "^4.13.3", 8 | "gulp": "^3.9.0", 9 | "gulp-ng-config": "^1.2.1" 10 | }, 11 | "devDependencies": {}, 12 | "scripts": { 13 | "start": "node index", 14 | "test": "echo \"Error: no test specified\" && exit 1" 15 | }, 16 | "repository": { 17 | "type": "git", 18 | "url": "git+https://github.com/kudresov/angular-config-vars.git" 19 | }, 20 | "keywords": [ 21 | "angularjs" 22 | ], 23 | "author": "Vitalij Kudresov (http://kudresov.com)", 24 | "license": "ISC", 25 | "bugs": { 26 | "url": "https://github.com/kudresov/angular-config-vars/issues" 27 | }, 28 | "homepage": "https://github.com/kudresov/angular-config-vars#readme" 29 | } 30 | --------------------------------------------------------------------------------