npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

nodeconfig

v0.3.1

Published

An event-based configuration provider for Node.js programs

Downloads

4

Readme

NodeConfig

NodeConfig is a library which provides the programmer with a means to inject configuration parameters into their program. It can serve as an alternative to loading and parsing config files.

Installing

npm install nodeconfig

Or, to obtain the development version, directly from git:

npm install git+https://[email protected]/closure-poland/nodeconfig.git

Usage - configured application

NodeConfig is an event-based configuration injector. All ConfigProvider objects are EventEmitters. This means that your application waits for a config to appear before starting its services, like so:

// Assume that "provider" is an object - more about it below.
provider.once('config', function initializeApplication(configObject){
  server.listen(configObject.http.port);
});

Notice how the example above uses 'once'. That is because the application only supports a one-time initialization - it would be an error to call .listen() on the same server object twice in a row. Were we to implement dynamic configuration refreshing for an HTTP server, it might look like the snippet below:

var listening = false;
function startListening(port){
	if(listening){
		server.close();
	}
    server.listen(port);
	listening = true;
}

provider.on('config', function updateConfig(newConfig){
    startListening(newConfig.http.port);
});

Moreover, we can only listen to interesting keys of the config - instead of using 'config' as the event name, we can write:

provider.on('config.http.port', function configurePort(newPort){
    startListening(newPort);
});

Usage - configuration passing

OK, so we've written an application which accepts configuration parameters from some "provider" object. But how do we get one to use, in the first place? By constructing one of the provided (ahem) providers, of course!

Configuration providers

(Note: right now, only two config providers are available. Stay tuned for more - patches are welcome!)

EnvironmentConfigProvider

Initialization:

var NodeConfig = require('nodeconfig');
var provider = new NodeConfig.EnvironmentConfigProvider();

Config passing:

env 'config.http.port=number:1337' 'config.http.banner=string:Hello, world!' node YourServer.js

(Or you can use Node's child_process to spawn another Node instance and pass the config variables)

JSONFileConfigProvider

Initialization:

var NodeConfig = require('nodeconfig');
var provider = new NodeConfig.JSONFileConfigProvider('/path/to/file.json');

No further config passing is needed, of course. This provider emits error events in case something goes wrong, so make sure to listen for them or be prepared for uncaught exeptions!

provider.on('error', function handleError(error) {
	// handle error here
});

Development roadmap

One of the first features to implement next will be alternative configuration providers (including some that actually support configuration refreshing) and working test cases for the ConfigurationProvider's EventEmitter interface.

A code refactoring would also probably be useful along the way, so that config tree splitting on dots and mass-publishing happens in another layer. This is not trivial, since, at some point, I would like to add config deduplication (i.e. only emitting changed keys).