A simple multiple-configuration management module.
npm install configuremyConfig.json file:
json
{
"serverPort" : 3000,
"couchDb" : {
"host" : "localhost",
"port" : 5984,
}
}
`
The main.js file:
`javascript
var config = require("configure");
var port = 2000; // default port
if(config.serverPort) {
port = config.serverPort;
}
// elsewhere...
server.listen(port, requestHandler);
`
The database.js file:
`javascript
var config = require("configure");
var request = { "host":"my.couchdb.com", "port":5984 };
if(config.couchDb) {
if(config.couchDb.host) {
request.host = config.couchDb.host;
}
if(config.couchDb.port) {
request.port = config.couchDb.port;
}
}
// later...
request.path = "/mydata";
http.get(request, responseHandler);
`
The node start script:
node main.js --config myConfig.json
#The Config File
At present _node-configure_ only supports JSON configuration files.
#Default Behavior
The first time the _node-configure_ module is required by an application, it will attempt to load the file specified
by the --config switch relative to the current working directory as obtained via process.cwd(). If
_node-configure_ fails to find or load the file, it will throw an exception.
If the --config switch is not included as a command line parameter, _node-configure_ will attempt to load the file
"config.json" in the current working directory. If that file is not found, _node-configure_ will throw an exception.
#Changing Default Behavior
_node-configure_ makes use of npm's
package-level configuration system. If you wish to
change the default behavior of _node-configure_ you may do so through this system. After changing a package
configuration option via npm config set, you must restart the _node-configure_ package to use the new settings.
For example:
npm config set configure:notFound throw
npm restart configure
_node-configure_ supports the following npm configuration keys:
* notFound: specifies what _node-configure_ should do when it fails to load a configuration file. Set this value
to "throw" to cause _node-configure_ to throw an exception on a failed load. Any other value will cause _node-configure_
to return null when it fails to load a configuration file.
* defaultConfigFile: specifies the file _node-configure_ should attempt to load if no file is specified via
command line.
* commandLineSwitchName: specifies the command line switch _node-configure_ should look for to determine which
configuration file to load. Change this value if you or some other module already use --config`