sn-props

1.0.1 • Public • Published

sn-props

Read Application Properties from local files or http resources (formerly node-props)

This package lets application users specify one or more URLs to JSON files containing "application properties." After the files are read and deserialized, a programmer supplied callback is executed. The module provides a read() function which reads the command line arguments looking for the string '--config'. The parameter following the config file is a file:, http: or https: URI.

You can use multiple --config flags to specify multiple resources. Each resource will be read in order and combined into a single object that is passed to the callback.

I use this feature to separate hardware specific properties from application specific properties. I usually have hardware specific properties (like IP addresses and ports to listen on) in files on the filesystem while application specific properties (names of DB servers, application timeouts, etc.) in http resources on our internal net. This is useful if you're running the same application on several different machines.

Installation

The easiest way to install this package is to use npm:

    npm install sn-props

If you want to check out the source, use the git command:

    git clone git://github.com/smithee-us/sn-props.git

Usage (for end users)

This package is intended to allow a node application user to do something like this:

    node application.js \
      --config file:///etc/application.json \
      --config http://example.com/application.json

and then have the system will pull properties from both the local file and the remote server (example.com), munge them together in the same object and start the main body of the application.

The sn-props module will also scan the command line for other parameters (identified by a leading "--".) Values following the parameter will be passed to the props.read() call in the properties object. For instance:

    node application.js --port 9001 --host 127.0.0.1

will pass the following object to the program through the props.read() call:

{
  "port": "9001",
  "host": "127.0.0.1"
}

Placing a slash ("/") in a property name will cause that property name to be interpreted as a path into the properties object passwd with the props.read() call. The following invocation:

    node application.js\
      --name "Ragnarok Server"\
      --listen/host 127.0.0.1\
      --listen/port 9001

will pass the following object to the program through the props.read() call:

{
  "name": "Ragnarok Server",
  "listen": {
    "port": "9001",
    "host": "127.0.0.1"
  }
}

Usage (for developers)

To use the module, simply import the package can call the read() function. Ex:

    var props = require( 'sn-props' );
    props.read( function ( properties ) {
      console.log( 'started this app with the following props:');
      console.log( JSON.stringify( properties ) );
    } );

The module uses "reasonable" defaults for http and https connections, but these can be overridden by passing options to a new props object and calling the read() function on it directly. For instance, the 'global' option defines a global options file that is read after resources specified on the command line. This code fragment reads properties from files or URLs specified in the command line and then reads them from the file "/etc/sample.json".

    var props = require( 'sn-props' );
    var props_options = { global: "file:///etc/sample.json" };

    ( new props( props_options ) ).read( function( properties ) {
      console.log( 'started this app with the following props:');
      console.log( JSON.stringify( properties ) );
    } );

License

This package is released under a MIT license, the text of which may be found at https://github.com/smithee-us/sn-props/raw/master/LICENSE .

Package Sidebar

Install

npm i sn-props

Weekly Downloads

0

Version

1.0.1

License

none

Last publish

Collaborators

  • meadhbh