Answer the question
In order to leave comments, you need to log in
What is the best way to organize local settings in an angular project?
The project develops a server part in the form of an API, to which an angular project is attached. In the local version, for example, you need to specify 1 path to the api, in production another.
For the frontend, I want to have:
Answer the question
In order to leave comments, you need to log in
Thanks for the replies, they led me to a solution. I did this: I
took the project structure generated by generator-gulp-angular as a basis.
At the root, I created the config folder, which contains json files with settings. The default.json describes all kinds of settings. In other files, you just need to redefine the desired field:
// config/default.json
{
"api": {
"scheme": "http",
"host": "localhost",
"port": 8080,
"root": "/api/v1/"
}
}
// config/prod.json
{
"api": {
"host": "example.com"
}
}
// config/dev.json
{}
// gulp/config.js
'use strict';
var gulp = require('gulp');
var gutil = require('gulp-util');
var cjson = require('cjson');
var b2v = require('buffer-to-vinyl');
var gulpNgConfig = require('gulp-ng-config');
module.exports = function (options) {
function p(name) {
return [options.config + '/default.json', options.config + '/' + name + '.json'];
}
gulp.task('config', function () {
var env = gutil.env.env || 'dev'; // получаем имя окружения через значение аргумента командной строки, или выставляем его равным dev, если ничего не передавалось
var config = JSON.stringify({
config: cjson.load(p([env]), true) // Загружаем json'ы, сливая их в 1 объект
});
return b2v.stream(new Buffer(config), 'config.js') // создаём поток из строки
.pipe(gulpNgConfig('lemonfront.config', { // генерим по JSON'у ангуляровский модуль
createModule: false
}))
.pipe(gulp.dest(options.tmp + '/serve/app/')); // и записываем во временную папку
});
}
// .tmp/serve/app/config.js
angular.module('app.config')
.constant('config', {
"api": {
"scheme": "http",
"host": "localhost",
"port": 8080,
"root": "/api/v1/"
}
});
// src/app/index.js
angular.module('app.config', []);
angular.module('lemonfront', [
'ngAnimate',
'ngCookies',
'ngSanitize',
'ui.router',
'app.config',
'app.core'
]);
angular.module('...', [])
.factory(SomeService, ['config', function (config) {
console.log(config.api.host);
}]);
gulp build --env=prod
There is such a thing yeoman.io/generators and the actual generator itself https://github.com/DaftMonk/generator-angular-fullstack
I really like the organized structure and use of the grunt command
. The dev project will start, with debugs and other goodies.
Start production
To add new modules, just use the commands:
bower install --save module-name
npm install --save module-name
$ yo angular-fullstack:endpoint post
[?] What will the url of your endpoint to be? /api/posts
make a config.dev.js file or something like that and write something like this there:
angular.module('app.config', [])
.constant('API_ENDPOINT', 'http://api-dev.example.com')
;
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question