Clean configuration defaults via Object.create in Node.js

While recently coming back to clean up some Node.js module
code I was looking for a nicer way to handle configuration of an object besides
doing something like this to handle both configuration options and defaults.

var MyModule = function(opts){
  this.foo = opts.foo || 1;
  this.bar = opts.bar || 2;
  // the list goes on
}

In my opinion this code is not really readable, and defaults are not as
obvious as I would like them to be. So I decided to let myself get inspired from
jQuery which provides a nice way to handle settings and defaults in plugins, via
$.extend(defaults, options) so my first
Idea was to simple create an extend function in node, but since I would have to
extend Object to make this as clean as possible I was looking for a way to
handle this natively, and this is where
Object.create
comes in handy.

Object.create allows creating of an object based on some prototype as well as
some default values, which is exactly what I was looking for. Do to the way
Object.create handles properties the options need to be prepared before merging
them in the configuration, which is finally bound to this.

var MyModule = function(opts){
  // some default values 
  var defaults = { foo: 1, bar: 2 };

  // prepare the options for Object.create
  var options = {};
  for(var i in opts){
    options[i] = { 
      value: opts[i], 
      enumerable: true, 
      writeable: true, 
      configurable: true } 
    };
  }

  // let Object.create merge the options with the defaults
  var config = Object.create(defaults, options);

  // bind to this
  for(var o in config){
    this[o] = config[o];
  }
}

Any suggestions for a different cleaner handling of configuration options are
highly welcome.

EDIT: Fixed extra level of nesting in options thanks to Sami Samhuri

Networking with NodeJS @munichjs

Yesterday I did a quick talk about ‘Lower’ level networking applications with
nodejs at munichjs. Lower in this
case means sending binary Data via a UDP/TCP socket, handle DNS resolving and
such. Interested? Talk a look at the slides

Examples can be found on github like Nodrrr to use growl from nodejs, the DNS channel patch for node and also even though it is alpha an will be broken from time to time the RDNS resolver

Slimy Vim

Since taking a look at clojure again I was looking
over at Emacs thinking, even though it feels unusable to me it has the nice
feature of evaluating an expression right here and now. Since I’m kind of
invested in both Vim as well as Textmate, with Vim being my current favorite,
I was looking around to get similar. What I came across first was running the
VimClojure Plugin which includes the Nailgunserver, running a single clojure instance evaluating the code you sent to it.
Even though it feels nice to use it mainly works for clojure and is kind of
heavy weight to set up. A simpler solution was described in a post about setting
up clojure on Vim and uses slime to pass
code to a screen window running some interpreter. The nice thing is it works the
same for ruby, python, nodejs, clojure, etc.. My current setup is available found at
Github

Set DNS servers in NodeJS on a per channel bases

In NodeJS DNS request are handled in the background via the ares library to enable asynchronous requesting. Ares allows the DNS server to be used set on a per channel bases, but this is not exposed in NodeJS, but is is quite simple to enable it by patching node_cares.cc. In essence this enables setting the DNS server via the parameter CHANNEL_NS on creation. To expose this towards NodeJS there needs to be an extension on the default dns.js module. The reason for me to enable this was to use different nameservers on each request, so the extension creates a new channel each time a new nameserver is needed.
I packed everything in a Github repository including some examples.