Javascript 在浏览器中使用nconf?
我希望在浏览器中使用。我曾尝试将其与browserify一起使用,但由于nconf在需要扫描目录进行配置时调用fs.readdirSync,因此在浏览器中失败Javascript 在浏览器中使用nconf?,javascript,configuration,nconf,Javascript,Configuration,Nconf,我希望在浏览器中使用。我曾尝试将其与browserify一起使用,但由于nconf在需要扫描目录进行配置时调用fs.readdirSync,因此在浏览器中失败 // config.js 'use strict'; var nconf = require('nconf'); // this triggers the fs.readdirSync var globalConfig = { my: { global: 1 }}; nconf.use('global', { type: 'liter
// config.js
'use strict';
var nconf = require('nconf'); // this triggers the fs.readdirSync
var globalConfig = { my: { global: 1 }};
nconf.use('global', { type: 'literal', store: globalConfig });
var envConfig = { my: { env: 2 }};
nconf.use('env', { type: 'literal', store: envConfig });
module.exports = nconf;
是否可以使用某种browserify转换(我没有看到强制在nconf中使用BRF的方法)或使用nconf(或其他类似库)来管理客户端配置
如果不是nconf本身,那么就让我做一些类似的事情:
config.load('user-settings', { my : { user: 1 } });
config.load('global-settings', { my: { global: 2 } } );
config.get('my.user'); // 1
config.get('my.global'); // 2
config.unload('global-settings');
config.get('my.global'); // undefined
我最近也有这个问题。我决定把自己的配置文件放在一起,而不是放进另一个库。以下是我最终得到的结果:
/*
This file determines which environment we're in, and provides the app with the appropriate config
*/
export const defaults = {
searchURI: 'http://www.google.com/',
anotherURI: 'https://stackoverflow.com/',
};
export const dev = {
searchURI: 'https://www.bing.com/',
};
export const test = {
searchURI: 'https://www.yahoo.com/',
};
export const prod = {
searchURI: 'https://duckduckgo.com/',
};
const config = () => {
switch (process.env.YOUR_ENV_NAME) {
case 'dev':
return dev;
case 'test':
return test;
default:
return prod;
}
};
export default {
...defaults,
...config(),
};
使用此模式,您可以像导入NCOF一样导入配置:
import config from './path/to/above/file.js';
const { searchURI, anotherURI } = config;
// OR
const searchURI = config.searchURI;
const anotherURI = config.anotherURI;
有这样一个问题:但是它不允许插入您自己的格式化程序,所以您不能使用yaml作为例子。还有browserify fs,但我还不能确定如何让它覆盖browserify捆绑包中的fs require脚本。