<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss xmlns:atom="http://www.w3.org/2005/Atom" xmlns:openSearch="http://a9.com/-/spec/opensearch/1.1/" xmlns:blogger="http://schemas.google.com/blogger/2008" xmlns:georss="http://www.georss.org/georss" xmlns:gd="http://schemas.google.com/g/2005" xmlns:thr="http://purl.org/syndication/thread/1.0" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" version="2.0"><channel><atom:id>tag:blogger.com,1999:blog-15643421</atom:id><lastBuildDate>Fri, 03 May 2013 20:28:09 +0000</lastBuildDate><category>Personal</category><category>Visual Studio</category><category>Microsoft</category><category>Behavior-Driven Development</category><category>Architecture</category><category>Family</category><category>SourceControl</category><category>Core Skills</category><category>Castle</category><category>Windows</category><category>ASP.NET</category><category>Web</category><category>C++</category><category>WF</category><category>Node.js</category><category>Community</category><category>DirectoryServices</category><category>Software Design</category><category>StructureMap</category><category>Humor</category><category>AutoMapper</category><category>Design Patterns</category><category>Windows Forms</category><category>Continuous Integration</category><category>Domain-Driven Design</category><category>Music</category><category>ALT.NET</category><category>Java</category><category>NoSQL</category><category>Open Source</category><category>CouchDB</category><category>E-VAN</category><category>Conferences</category><category>CoffeeScript</category><category>NServiceBus</category><category>Agile</category><category>WCF</category><category>Ruby</category><category>Database</category><category>Linux</category><category>Test-Driven Development</category><category>NHibernate</category><category>Tools</category><category>Object-Relational Mapping</category><category>JavaScript</category><category>WPF</category><category>Fluent Interfaces</category><category>.NET</category><category>Concurrent Programming</category><category>Books</category><title>Share the intellectual wealth</title><description /><link>http://vanryswyckjan.blogspot.com/</link><managingEditor>noreply@blogger.com (Jan Van Ryswyck)</managingEditor><generator>Blogger</generator><openSearch:totalResults>426</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>25</openSearch:itemsPerPage><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/ShareTheIntellectualWealth" /><feedburner:info uri="sharetheintellectualwealth" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><item><guid isPermaLink="false">tag:blogger.com,1999:blog-15643421.post-6131925605658526252</guid><pubDate>Fri, 03 May 2013 20:28:00 +0000</pubDate><atom:updated>2013-05-03T22:28:09.229+02:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Node.js</category><title>Introducing node-validation</title><description>&lt;p align="justify"&gt;Some time ago I was looking for a validation library/module for use in a small &lt;a href="http://vanryswyckjan.blogspot.be/2011/12/taking-toddler-steps-with-nodejs.html" target="_blank"&gt;Express&lt;/a&gt; application that I was writing at the time. I couldn’t find anything that suited my taste so I decided to write one myself just for kicks. The goal was learning how to publish a module to &lt;a href="https://npmjs.org/" target="_blank"&gt;npm&lt;/a&gt; and making a futile attempt to contribute something back to the vibrant Node.js community. &lt;a href="https://github.com/JanVanRyswyck/node-validation" target="_blank"&gt;node-validation&lt;/a&gt; is a minimal but slightly opinionated validation library for Node.js. &lt;/p&gt; &lt;p align="justify"&gt;Installing node-validation can be done using the canonical package manager:&lt;/p&gt; &lt;blockquote&gt; &lt;p align="justify"&gt;&lt;font style="background-color: #ffffff"&gt;&lt;em&gt;$ npm install node-validation&lt;/em&gt;&lt;/font&gt;&lt;/p&gt;&lt;/blockquote&gt; &lt;p align="justify"&gt;Validation rules must be defined in a custom validator by deriving from the base V&lt;em&gt;&lt;code&gt;&lt;em&gt;alidator&lt;/em&gt;&lt;/code&gt;.&lt;/em&gt;&lt;/p&gt;&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;var&lt;/span&gt; MyObjectValidator = &lt;span class="kwrd"&gt;function&lt;/span&gt;() {
    Validator.call(&lt;span class="kwrd"&gt;this&lt;/span&gt;);

    &lt;span class="kwrd"&gt;this&lt;/span&gt;.ruleFor(&lt;span class="str"&gt;'stringProperty'&lt;/span&gt;).isNotEmpty();
    &lt;span class="kwrd"&gt;this&lt;/span&gt;.ruleFor(&lt;span class="str"&gt;'otherStringProperty'&lt;/span&gt;).hasMaximumLength(10);

    &lt;span class="kwrd"&gt;this&lt;/span&gt;.ruleFor(&lt;span class="str"&gt;'numericStringProperty'&lt;/span&gt;).isNumber()
        .withMessage(&lt;span class="str"&gt;'Oops, something is wrong ...'&lt;/span&gt;);
    &lt;span class="kwrd"&gt;this&lt;/span&gt;.ruleFor(&lt;span class="str"&gt;'dateStringProperty'&lt;/span&gt;)
        .matches(/^(19|20)\d\d[-](0[1-9]|1[012])[-](0[1-9]|[12][0-9]|3[01])$/);

    &lt;span class="kwrd"&gt;this&lt;/span&gt;.ruleFor(&lt;span class="str"&gt;'numberProperty'&lt;/span&gt;).isInteger();
    &lt;span class="kwrd"&gt;this&lt;/span&gt;.ruleFor(&lt;span class="str"&gt;'otherNumberProperty'&lt;/span&gt;).isMaximum(5);

    &lt;span class="kwrd"&gt;this&lt;/span&gt;.ruleFor(&lt;span class="str"&gt;'exoticProperty'&lt;/span&gt;).&lt;span class="kwrd"&gt;is&lt;/span&gt;(&lt;span class="kwrd"&gt;function&lt;/span&gt;(value) {
        &lt;span class="kwrd"&gt;return&lt;/span&gt; 3 === value.propertyA + value.propertyB;
    }).withMessage(&lt;span class="str"&gt;'Either propertyA or propertyB has an incorrect value.'&lt;/span&gt;);
};

util.inherits(MyObjectValidator, Validator);&lt;/pre&gt;
&lt;p align="justify"&gt;After creating a validator object, an object that needs to be validated (the subject) can be passed to the &lt;code&gt;&lt;em&gt;validate&lt;/em&gt;&lt;/code&gt; method. The &lt;code&gt;&lt;em&gt;validate&lt;/em&gt;&lt;/code&gt; method returns an array of validation errors specifying a message and the name of the violating property.&lt;/p&gt;&lt;pre class="csharpcode"&gt;&lt;span class="rem"&gt;//&lt;/span&gt;
&lt;span class="rem"&gt;// Validation subject&lt;/span&gt;
&lt;span class="rem"&gt;//&lt;/span&gt;
&lt;span class="kwrd"&gt;var&lt;/span&gt; subject = {
    stringProperty: &lt;span class="str"&gt;''&lt;/span&gt;,
    otherStringProperty: &lt;span class="str"&gt;'Some string value that is too long ...'&lt;/span&gt;,

    numericStringProperty: &lt;span class="str"&gt;'65.85 invalid'&lt;/span&gt;,
    dateStringProperty: &lt;span class="str"&gt;'2013-04-30 invalid'&lt;/span&gt;,

    numberProperty: &lt;span class="str"&gt;'Some invalid number'&lt;/span&gt;,
    otherNumberProperty: 48,

    exoticProperty: {
        propertyA: 1,
        propertyB: 1
    }
};

&lt;span class="rem"&gt;//&lt;/span&gt;
&lt;span class="rem"&gt;// Now it's time to validate&lt;/span&gt;
&lt;span class="rem"&gt;//&lt;/span&gt;
&lt;span class="kwrd"&gt;var&lt;/span&gt; validator = &lt;span class="kwrd"&gt;new&lt;/span&gt; MyObjectValidator();
&lt;span class="kwrd"&gt;var&lt;/span&gt; validationErrors = validator.validate(subject);

&lt;span class="kwrd"&gt;for&lt;/span&gt;(&lt;span class="kwrd"&gt;var&lt;/span&gt; i=0; i &amp;lt; validationErrors.length; i++) {
    console.log(&lt;span class="str"&gt;'Property name: '&lt;/span&gt; + validationErrors[i].propertyName 
                + &lt;span class="str"&gt;', Message: '&lt;/span&gt; + validationErrors[i].message);
}&lt;/pre&gt;
&lt;style type="text/css"&gt;.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
&lt;/style&gt;

&lt;p&gt;There you go. Head over to &lt;a href="https://github.com/JanVanRyswyck/node-validation" target="_blank"&gt;the GitHub repository&lt;/a&gt; and give it a try. I’m definitely looking forward to hear your feedback. &lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/ShareTheIntellectualWealth/~4/CDApJN_9Sv8" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/ShareTheIntellectualWealth/~3/CDApJN_9Sv8/introducing-node-validation.html</link><author>noreply@blogger.com (Jan Van Ryswyck)</author><thr:total>0</thr:total><feedburner:origLink>http://vanryswyckjan.blogspot.com/2013/05/introducing-node-validation.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-15643421.post-6941346001820328275</guid><pubDate>Fri, 22 Mar 2013 22:16:00 +0000</pubDate><atom:updated>2013-03-22T23:16:41.663+01:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">JavaScript</category><title>Basic JavaScript: Prototypical Inheritance vs. Functional Inheritance</title><description>&lt;p align="justify"&gt;Inheritance in JavaScript has been the topic of many discussions in the past and will continue to be the source of future debates and arguments. While we do value &lt;a href="http://en.wikipedia.org/wiki/Composition_over_inheritance" target="_blank"&gt;composition over inheritance&lt;/a&gt;, we don’t want to throw the baby out with the bathwater either. So, from time to time, we run into these cases where we want some notion of inheritance in JavaScript. Now what? &lt;/p&gt; &lt;p align="justify"&gt;As with many things in JavaScript, there is not a single straight answer. We can choose between a couple of options and many different variations of these solutions. But one thing’s for sure: we can’t have it all!&lt;/p&gt; &lt;p align="justify"&gt;In this blog post I want to discuss two different styles of inheritance that I have a hard time choosing from when programming JavaScript. And as with everything in life, both styles have their own pros and cons.&amp;nbsp; &lt;/p&gt; &lt;h4&gt;Prototypical inheritance&lt;/h4&gt; &lt;p align="justify"&gt;In ‘classical’ programming languages, one class can directly inherit from another class. JavaScript doesn’t have this notion of classes (yet). Instead, JavaScript has prototypes which you can augment to fit your own needs. This means that having a single augmented object as the prototype for other objects, which &lt;em&gt;‘inherit’&lt;/em&gt; all members of the augmented prototype object, kind of simulates a &lt;em&gt;pseudo-classical&lt;/em&gt; &lt;em&gt;inheritance&lt;/em&gt; pattern. Let’s talk code in order to demystify this concept.&lt;/p&gt;&lt;pre class="csharpcode" style="height: 641px; width: 100%"&gt;&lt;span class="rem"&gt;// validator.js&lt;/span&gt;
&lt;span class="kwrd"&gt;var&lt;/span&gt; Validator = exports.Validator = &lt;span class="kwrd"&gt;function&lt;/span&gt;() {
    &lt;span class="kwrd"&gt;this&lt;/span&gt;._rules = [];
};

Validator.prototype.addRule = &lt;span class="kwrd"&gt;function&lt;/span&gt;(rule) {
    &lt;span class="kwrd"&gt;this&lt;/span&gt;._rules.push(rule)
};

Validator.prototype.validate = &lt;span class="kwrd"&gt;function&lt;/span&gt;(instance) {
    ...
};

&lt;span class="rem"&gt;// specificValidator.js&lt;/span&gt;
&lt;span class="kwrd"&gt;var&lt;/span&gt; util = require(&lt;span class="str"&gt;'util'&lt;/span&gt;);

&lt;span class="kwrd"&gt;var&lt;/span&gt; SpecificValidator = &lt;span class="kwrd"&gt;function&lt;/span&gt;() {
    Validator.call(&lt;span class="kwrd"&gt;this&lt;/span&gt;);
};

util.inherits(SpecificValidator, Validator);

SpecificValidator.prototype.filter = &lt;span class="kwrd"&gt;function&lt;/span&gt;(instance) {
    ...
};

&lt;span class="rem"&gt;// client.js&lt;/span&gt;
&lt;span class="kwrd"&gt;var&lt;/span&gt; validator = &lt;span class="kwrd"&gt;new&lt;/span&gt; SpecificValidator();

&lt;span class="rem"&gt;// Calls function on derived object&lt;/span&gt;
validator.filter( { ... } );        

&lt;span class="rem"&gt;// Calls function on base object&lt;/span&gt;
validator.validate( { ... } );        &lt;/pre&gt;
&lt;style type="text/css"&gt;.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
&lt;/style&gt;

&lt;p align="justify"&gt;Here we have a constructor function named &lt;em&gt;Validator&lt;/em&gt; which is the base object for other &lt;em&gt;‘derived’&lt;/em&gt; objects. We augment the prototype with two functions (&lt;em&gt;addRule&lt;/em&gt; and &lt;em&gt;validate&lt;/em&gt;). Next we define another constructor function named &lt;em&gt;SpecificValidator&lt;/em&gt;. We &lt;em&gt;‘derive’&lt;/em&gt; this new&amp;nbsp; constructor function by calling the base constructor function and wiring the prototype by using the &lt;a href="http://nodejs.org/api/util.html#util_util_inherits_constructor_superconstructor" target="_blank"&gt;util.inherits()&lt;/a&gt; function from the Node.js core library.&lt;/p&gt;
&lt;p align="justify"&gt;We have to use the &lt;em&gt;new&lt;/em&gt; keyword in order to instantiate a &lt;em&gt;SpecificValidator&lt;/em&gt; object. Now we can use the functions that we added to the prototype.&lt;/p&gt;
&lt;h4&gt;Functional inheritance&lt;/h4&gt;
&lt;p align="justify"&gt;This pattern is advocated by &lt;a href="http://www.crockford.com/" target="_blank"&gt;Douglas Crockford&lt;/a&gt; in his book &lt;a href="http://vanryswyckjan.blogspot.be/2010/05/book-review-javascript-good-parts.html" target="_blank"&gt;JavaScript, The Good Parts&lt;/a&gt;. There he offers this particular style as the way to go for inheriting objects. Let’s look at an example.&lt;/p&gt;&lt;pre class="csharpcode" style="height: 676px; width: 100%"&gt;&lt;span class="rem"&gt;// validator.js&lt;/span&gt;
module.exports = &lt;span class="kwrd"&gt;function&lt;/span&gt;() {
    &lt;span class="kwrd"&gt;var&lt;/span&gt; rules = [], my = {};

    my.addRule = &lt;span class="kwrd"&gt;function&lt;/span&gt;(rule) {
        rules.push(rule);
    };

    my.validate = &lt;span class="kwrd"&gt;function&lt;/span&gt;(instance) {
        ...
    };

    &lt;span class="kwrd"&gt;return&lt;/span&gt; my;
};

&lt;span class="rem"&gt;// specificValidator.js&lt;/span&gt;
&lt;span class="kwrd"&gt;var&lt;/span&gt; validator = require(&lt;span class="str"&gt;'...'&lt;/span&gt;).validator;

&lt;span class="kwrd"&gt;var&lt;/span&gt; specificValidator = &lt;span class="kwrd"&gt;function&lt;/span&gt;() {
    &lt;span class="kwrd"&gt;var&lt;/span&gt; my = validator();

    my.filter = &lt;span class="kwrd"&gt;function&lt;/span&gt;(instance) {
        ...
    };
    
    &lt;span class="kwrd"&gt;return&lt;/span&gt; my;
};

&lt;span class="rem"&gt;// client.js&lt;/span&gt;
&lt;span class="kwrd"&gt;var&lt;/span&gt; validator = specificValidator();

&lt;span class="rem"&gt;// Calls function on derived object&lt;/span&gt;
validator.filter( { ... } );    

&lt;span class="rem"&gt;// Calls function on base object&lt;/span&gt;
validator.validate( { ... } );        &lt;/pre&gt;
&lt;style type="text/css"&gt;.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
&lt;/style&gt;

&lt;p align="justify"&gt;The base constructor function returns an object that is augmented with functions and is returned at the end. The derived constructor function simple calls the base constructor function and further augments the retrieved object before returning it to the calling code. Here we don’t have to use the new keyword to instantiate anything. Just calling the right constructor function gives us an object which we can use in our client code. &lt;/p&gt;
&lt;h4&gt;Conclusion&lt;/h4&gt;
&lt;p align="justify"&gt;The most important benefit of prototypical inheritance, at least in my humble opinion, is performance. By augmenting the prototype with functions, we only create these functions once. Not matter how many times we instantiate a constructor function, the same functions get (re)used every single time. Functional inheritance on the other hand creates new functions every time a constructor function is called, which is several orders of magnitude slower compared to the prototypical inheritance pattern.&lt;/p&gt;
&lt;p align="justify"&gt;On the other hand, the prototypical approach doesn’t come with encapsulation. Looking at the example shown earlier, the ‘_rules’ property is publicly available to the client code and can be manipulated at will. By using a simple convention, like prefixing with an underscore, we can indicate that these private members should not be touched in order to guarantee a correct behavior. But again, nothing can be enforced. Using functional constructors, we can have private variables and functions that cannot be manipulated by the calling code.&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/p&gt;
&lt;p align="justify"&gt;There are more pros and cons, but for me, these are the most important ones to be aware of. You can see that both styles have their strengths and weaknesses. I usually tend to go with prototypical inheritance as this is the ‘JavaScript way’, but I like using the functional approach as well for those cases were I know in advance that not too many objects are created or when I don’t care about performance.&lt;/p&gt;
&lt;p align="justify"&gt;I would love to hear other takes on this. What particular styles do you use? When do you use them and why?&lt;/p&gt;
&lt;p align="justify"&gt;Until next time.&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/ShareTheIntellectualWealth/~4/pdtpzI3POAw" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/ShareTheIntellectualWealth/~3/pdtpzI3POAw/basic-javascript-prototypical.html</link><author>noreply@blogger.com (Jan Van Ryswyck)</author><thr:total>0</thr:total><feedburner:origLink>http://vanryswyckjan.blogspot.com/2013/03/basic-javascript-prototypical.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-15643421.post-2255686637800919708</guid><pubDate>Fri, 22 Feb 2013 20:46:00 +0000</pubDate><atom:updated>2013-02-22T21:46:23.214+01:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Tools</category><title>Confessions of a Sublime Text-aholic</title><description>&lt;p align="justify"&gt;It’s true. I’m a Sublime Text addict. It’s by far my favorite development tool. End of story!&lt;/p&gt; &lt;p align="justify"&gt;Just to illustrate, earlier this week, a member of our development team asked how to quickly remove all empty lines from a very large text file. I quickly came up with the following:&lt;/p&gt; &lt;ol&gt; &lt;li&gt; &lt;div align="justify"&gt;Press CTRL-F.&lt;/div&gt; &lt;li&gt; &lt;div align="justify"&gt;Enable regular expressions (the button entirely in the bottom-left corner).&lt;/div&gt; &lt;li&gt; &lt;div align="justify"&gt;Search for ^\s*$&lt;/div&gt; &lt;li&gt; &lt;div align="justify"&gt;Press ALT-ENTER (click on the “Find all” button).&lt;/div&gt; &lt;li&gt; &lt;div align="justify"&gt;Hit the backspace button.&lt;/div&gt; &lt;li&gt; &lt;div align="justify"&gt;Done!&lt;/div&gt; &lt;li&gt; &lt;div align="justify"&gt;Be merry …&lt;/div&gt;&lt;/li&gt;&lt;/ol&gt; &lt;p align="justify"&gt;Don’t just take my word for it. Just start using it!&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/ShareTheIntellectualWealth/~4/RI70sLZLfBA" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/ShareTheIntellectualWealth/~3/RI70sLZLfBA/confessions-of-sublime-text-aholic.html</link><author>noreply@blogger.com (Jan Van Ryswyck)</author><thr:total>0</thr:total><feedburner:origLink>http://vanryswyckjan.blogspot.com/2013/02/confessions-of-sublime-text-aholic.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-15643421.post-2292557972206123770</guid><pubDate>Fri, 08 Feb 2013 19:45:00 +0000</pubDate><atom:updated>2013-02-08T20:45:17.288+01:00</atom:updated><title>Writing Fast, Memory-Efficient JavaScript</title><description>&lt;p align="justify"&gt;Earlier this week, I read this great article titled “&lt;a href="http://coding.smashingmagazine.com/2012/11/05/writing-fast-memory-efficient-javascript/" target="_blank"&gt;Writing Fast, Memory-Efficient JavaScript&lt;/a&gt;” by &lt;a href="http://addyosmani.com/blog/" target="_blank"&gt;Addy Osmani&lt;/a&gt;. This is a highly recommended read for anyone involved in writing JavaScript code.&lt;/p&gt; &lt;p align="justify"&gt;The topics that I found to be particularly interesting were the apparent fact that it’s better to avoid the delete keyword and cached functions in &lt;a href="http://vanryswyckjan.blogspot.be/2011/02/basic-javascript-part-10-module-pattern.html" target="_blank"&gt;the module pattern&lt;/a&gt;. The major down-side that I see when using cached functions is that you can’t have any private variables within your module. But this is highly interesting stuff, nonetheless.&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/ShareTheIntellectualWealth/~4/HSE5RZ0clyM" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/ShareTheIntellectualWealth/~3/HSE5RZ0clyM/writing-fast-memory-efficient-javascript.html</link><author>noreply@blogger.com (Jan Van Ryswyck)</author><thr:total>0</thr:total><feedburner:origLink>http://vanryswyckjan.blogspot.com/2013/02/writing-fast-memory-efficient-javascript.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-15643421.post-3217133096274689149</guid><pubDate>Fri, 01 Feb 2013 21:25:00 +0000</pubDate><atom:updated>2013-02-01T22:27:27.656+01:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Node.js</category><title>Taking Toddler Steps with Node.js - Express Routing Revisited</title><description>&lt;p align="justify"&gt;Last year I wrote &lt;a href="http://vanryswyckjan.blogspot.be/2012/01/taking-toddler-steps-with-nodejs.html" target="_blank"&gt;this blog post&lt;/a&gt; where I described a couple of ways on how to tackle routing with &lt;a href="http://expressjs.com/" target="_blank"&gt;Express&lt;/a&gt;. In the mean while I moved on from the “Plain Old School” approach to an approach where I replaced &lt;a href="http://underscorejs.org/" target="_blank"&gt;underscore.js&lt;/a&gt; with &lt;a href="https://github.com/TroyGoode/node-require-directory" target="_blank"&gt;node-require-directory&lt;/a&gt;. &lt;/p&gt; &lt;p align="justify"&gt;Setting up node-require-directory is quite easy. In the &lt;em&gt;routes&lt;/em&gt; folder, we just need to add an &lt;em&gt;index.js&lt;/em&gt; module with the following two lines:&lt;/p&gt;&lt;pre style="width: 100%; height: 41px" class="csharpcode"&gt;&lt;span class="kwrd"&gt;var&lt;/span&gt; requireDirectory = require(&lt;span class="str"&gt;'require-directory'&lt;/span&gt;);
module.exports = requireDirectory(module);&lt;/pre&gt;
&lt;style type="text/css"&gt;.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
&lt;/style&gt;

&lt;p align="justify"&gt;Setting up the routes for Express then looks like this:&lt;/p&gt;&lt;pre style="width: 100%; height: 169px" class="csharpcode"&gt;&lt;span class="kwrd"&gt;var&lt;/span&gt; routes = require(&lt;span class="str"&gt;'./../routes'&lt;/span&gt;);

&lt;span class="rem"&gt;// Setting up an application ...&lt;/span&gt;

application.get(&lt;span class="str"&gt;'/'&lt;/span&gt;, routes.root);
application.get(&lt;span class="str"&gt;'/home'&lt;/span&gt;, routes.home);
application.get(&lt;span class="str"&gt;'/signin'&lt;/span&gt;, routes.authentication.signin);
application.post(&lt;span class="str"&gt;'/signout'&lt;/span&gt;, routes.authentication.signout);

// More route registrations&lt;/pre&gt;
&lt;p align="justify"&gt;Here we simple reference the &lt;em&gt;index.js&lt;/em&gt; module. The node-require-directory module takes care of building up a tree of functions which we can now access for our route registrations. Adding a new route is as simple as creating a new module somewhere inside the routes folder or one of its subfolders and creating a new route registration. Have a look at &lt;a href="https://github.com/TroyGoode/node-require-directory/tree/master/test/example" target="_blank"&gt;this example&lt;/a&gt;.&lt;/p&gt;
&lt;p align="justify"&gt;I found this little gem to be quite useful and it might be helpful for some of you as well.&lt;/p&gt;
&lt;p align="justify"&gt;Until next time.&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/ShareTheIntellectualWealth/~4/l1k1fSVwvRQ" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/ShareTheIntellectualWealth/~3/l1k1fSVwvRQ/taking-toddler-steps-with-nodejs.html</link><author>noreply@blogger.com (Jan Van Ryswyck)</author><thr:total>0</thr:total><feedburner:origLink>http://vanryswyckjan.blogspot.com/2013/02/taking-toddler-steps-with-nodejs.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-15643421.post-2264824556446581494</guid><pubDate>Fri, 30 Nov 2012 21:10:00 +0000</pubDate><atom:updated>2012-11-30T22:10:18.688+01:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Tools</category><category domain="http://www.blogger.com/atom/ns#">.NET</category><category domain="http://www.blogger.com/atom/ns#">Visual Studio</category><title>Visual Studio Light Edition</title><description>&lt;p align="justify"&gt;I noticed this blog post from Scott Hanselman the other day about &lt;a href="http://www.hanselman.com/blog/FREEVisualStudioExpress2012ForWindowsDesktop.aspx" target="_blank"&gt;Visual Studio Express 2012 for Windows Desktop&lt;/a&gt;. This post included a screenshot from the installation program. Something that really took me by the throat is this:  &lt;p align="justify"&gt;&lt;a href="http://lh5.ggpht.com/-Qom8gh6OdA0/ULkgouhtD7I/AAAAAAAABtk/jZfjq0y9Pxs/s1600-h/image%25255B5%25255D.png"&gt;&lt;img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: block; float: none; border-top-width: 0px; border-bottom-width: 0px; margin-left: auto; border-left-width: 0px; margin-right: auto; padding-top: 0px" title="image" border="0" alt="image" src="http://lh6.ggpht.com/-C9c_6ovbwug/ULkgqC-8CqI/AAAAAAAABto/6LE1uOaNcDU/image_thumb%25255B3%25255D.png?imgmax=800" width="378" height="428"&gt;&lt;/a&gt;  &lt;p align="justify"&gt;Notice that the Express edition of Visual Studio seems to require no less than &lt;u&gt;4.15 GB&lt;/u&gt; of hard disk space! So I decided to try out the Professional edition of Visual Studio just to find out that it needs &lt;u&gt;7.56 GB&lt;/u&gt; to install!  &lt;p&gt;&lt;a href="http://lh3.ggpht.com/-tIWpHV0HNxQ/ULkgrJvZx5I/AAAAAAAABtw/fGaTj_wXmlM/s1600-h/image%25255B19%25255D.png"&gt;&lt;img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: block; float: none; border-top-width: 0px; border-bottom-width: 0px; margin-left: auto; border-left-width: 0px; margin-right: auto; padding-top: 0px" title="image" border="0" alt="image" src="http://lh4.ggpht.com/-GSh1pQV9Kpo/ULkgsdv9MtI/AAAAAAAABt4/plLoBUL6EhA/image_thumb%25255B13%25255D.png?imgmax=800" width="377" height="427"&gt;&lt;/a&gt;&lt;/p&gt; &lt;p align="justify"&gt;Ask yourself, how on earth can an IDE that requires 7.56 GB to install be fast enough to even be usable at all? What kind of monster machine is required to not even choke to death when I accidentally open a second instance of Visual Studio? We've all seen those dreadful white screens of death, right?  &lt;p align="justify"&gt;Yes, I know that we're living in 2012 and that disk space is very cheap. Yes, I know that RAM memory grow on trees these days and that CPU power is growing increasingly. But is this kind of footprint justified for an IDE that proclaims productivity? Or is it just me?  &lt;p align="justify"&gt;After recovering from my amazement I tried installing &lt;a href="http://www.icsharpcode.net/opensource/sd/" target="_blank"&gt;SharpDevelop&lt;/a&gt;, an open-source IDE for building .NET applications.  &lt;p align="justify"&gt;&lt;a href="http://lh6.ggpht.com/-NkxNQuBps7s/ULkgtBNUoUI/AAAAAAAABuA/uTF5tsPNjk8/s1600-h/image%25255B25%25255D.png"&gt;&lt;img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: block; float: none; border-top-width: 0px; border-bottom-width: 0px; margin-left: auto; border-left-width: 0px; margin-right: auto; padding-top: 0px" title="image" border="0" alt="image" src="http://lh5.ggpht.com/-lsIuOxaHpDQ/ULkguEID7eI/AAAAAAAABuI/-gXV0STPUic/image_thumb%25255B17%25255D.png?imgmax=800" width="424" height="327"&gt;&lt;/a&gt;  &lt;p align="justify"&gt;SharpDevelop seems to be more than happy with just a mere &lt;u&gt;64 Mb&lt;/u&gt; of storage. I tried opening a large solution and it loads pretty darn fast. So, I should just shut up and go with SharpDevelop then? See, this is where I have to admit that I have a slight problem.  &lt;p align="justify"&gt;See, I depend heavily on Resharper for doing C# development. This amazing code-by-keystrokes tool is both a blessing and a curse. Any serious developer who builds .NET applications has to admit that whipping up some C# code without a tool like Resharper is incredibly painful, to say the least. This tool is truly a blessing and this is why I can't go with SharpDevelop as my IDE of choice when building application for the .NET platform. But on the other hand, Resharper is not a stand-alone tool as it needs Visual Studio to host it. To me, this is a curse.  &lt;p align="justify"&gt;Visual Studio 2012 in it's current form has become too heavily packed with features, which I don't use anyway, so that's it's no longer usable for me to host Resharper. I'm no longer willing to make that tradeoff. I tried using Visual Studio 2012 with Resharper, but I turned back to Visual Studio 2010 after a short while. From a performance point of view, Visual Studio 2010 isn't running great either. So I guess this is just a necessary evil that I have to overcome for building .NET 4.0 applications.  &lt;p align="justify"&gt;I understand that Microsoft's business model is building platforms and tools for the world to use. It's a business like any other business, focused on earning money. There's nothing wrong with that. This is why Anders is so keen on &lt;a href="http://www.typescriptlang.org/" target="_blank"&gt;statically typed languages&lt;/a&gt;, because they require more tooling and tooling is what brings in the money for his division. It's as simple as that.  &lt;p align="justify"&gt;But it doesn't have to be all bad! What if the Visual Studio development team would come up with a stripped down version of Visual Studio? Think about how awesome this could be. What should be in the box? Just the basics! The code editor, the solution explorer, the debugger and the ability to host add-ons. No designers, no SQL Server integration, no TFS integration, etc. … Just the most basic features. That's it! The installation footprint should be no more than 500 Mb, ideally only 100Mb. Wouldn’t that be awesome? This would be would actually solve my current development needs!  &lt;p align="justify"&gt;I hereby tag my fellow Elegant Coder David Starr, who recently joined the Visual Studio team at Microsoft. You can do it David!&amp;nbsp; The best is yet to come!  &lt;p align="justify"&gt;So I'm eagerly awaiting the next release cycle of Visual Studio in order to see whether I'm up for a treat ;-).  &lt;p&gt;Until next time.&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/ShareTheIntellectualWealth/~4/X3zFx-Vjlk8" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/ShareTheIntellectualWealth/~3/X3zFx-Vjlk8/visual-studio-light-edition.html</link><author>noreply@blogger.com (Jan Van Ryswyck)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://lh6.ggpht.com/-C9c_6ovbwug/ULkgqC-8CqI/AAAAAAAABto/6LE1uOaNcDU/s72-c/image_thumb%25255B3%25255D.png?imgmax=800" height="72" width="72" /><thr:total>0</thr:total><feedburner:origLink>http://vanryswyckjan.blogspot.com/2012/11/visual-studio-light-edition.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-15643421.post-7601150960777353972</guid><pubDate>Tue, 07 Aug 2012 10:00:00 +0000</pubDate><atom:updated>2012-08-07T21:37:12.027+02:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Books</category><title>Steve Jobs - The Biography by Walter Isaacson</title><description>&lt;p&gt;&lt;a href="http://lh5.ggpht.com/-P5MmJFFUoMc/UBw921s5LXI/AAAAAAAABs0/vFzDvS7q7Rg/s1600-h/image%25255B4%25255D.png"&gt;&lt;img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: block; float: none; border-top-width: 0px; border-bottom-width: 0px; margin-left: auto; border-left-width: 0px; margin-right: auto; padding-top: 0px" title="image" border="0" alt="image" src="http://lh4.ggpht.com/-VZFjCPEWtj4/UBw9302clXI/AAAAAAAABs4/SqheLooKjuI/image_thumb%25255B2%25255D.png?imgmax=800" width="213" height="314"&gt;&lt;/a&gt;&lt;/p&gt; &lt;p align="justify"&gt;At first I was a bit skeptical whether I should spend time listening to the audio version of this book. I’ve heard and read both great things as well as bad things about the book. A few people recommended it, while a couple of others discouraged me from reading it. But after getting through only a few chapters I was completely hooked. &lt;/p&gt; &lt;p align="justify"&gt;The book tells the life story of Steve Jobs from birth until his sad passing, describing the important moments of his life in a very open and honest way. &lt;/p&gt; &lt;p align="justify"&gt;The part that I personally found the most interesting were the early years of Apple. There’s a lot of computer history in there that stems from when I was just an infant. These fascinating stories alone, like how the Mac and the IBM PC were rubbing shoulders, makes the book worthwhile. I’ve actually learned a lot from this book, especially about the events from the past that made Apple the company that it is today. &lt;/p&gt; &lt;p align="justify"&gt;I never really considered myself an Apple fan. Heck, I don’t even follow what they are announcing at their conferences or events. I usually read about it days after in the newspapers. But while I was reading the book, I started noticing all the Apple devices that me and my family are using on a day-to-day basis. When did that happen? Did they sneak up on me or what? When I told my wife about this, she told me that I start rambling about replacing my three year old desktop with an iMac every time we walk by an Apple store or a retailer. Wow! This certainly didn’t help either. Perhaps it’s inevitable, I don’t know.&lt;/p&gt; &lt;p align="justify"&gt;But what I do know is that I really enjoy using their devices (iPod, iPad, MacBook Pro, etc. …) and now I learned about the rationale behind it. And the scary part was that it all made sense as well. &lt;/p&gt; &lt;p align="justify"&gt;This book tells the remarkable story of a passionate individual with a firm vision that will be remembered for many generations to come. If you’re a technologist, you just owe it to yourself to pick up a copy and read it.&lt;/p&gt; &lt;p align="justify"&gt;Now I’m off reading a book on Bill Gates &lt;img style="border-bottom-style: none; border-left-style: none; border-top-style: none; border-right-style: none" class="wlEmoticon wlEmoticon-winkingsmile" alt="Winking smile" src="http://lh5.ggpht.com/-I-hpBaUwIhM/UBw945MJRHI/AAAAAAAABs8/sUARK73dog4/wlEmoticon-winkingsmile%25255B2%25255D.png?imgmax=800"&gt;.&lt;/p&gt; &lt;p align="justify"&gt;Until next time. &lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/ShareTheIntellectualWealth/~4/C1CH98rMqb0" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/ShareTheIntellectualWealth/~3/C1CH98rMqb0/steve-jobs-biography-by-walter-isaacson.html</link><author>noreply@blogger.com (Jan Van Ryswyck)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://lh4.ggpht.com/-VZFjCPEWtj4/UBw9302clXI/AAAAAAAABs4/SqheLooKjuI/s72-c/image_thumb%25255B2%25255D.png?imgmax=800" height="72" width="72" /><thr:total>1</thr:total><feedburner:origLink>http://vanryswyckjan.blogspot.com/2012/08/steve-jobs-biography-by-walter-isaacson.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-15643421.post-607781812159584863</guid><pubDate>Tue, 24 Jul 2012 10:00:00 +0000</pubDate><atom:updated>2012-07-24T12:00:15.120+02:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Tools</category><title>Blitz</title><description>&lt;p align="justify"&gt;The last couple of weeks I’ve been playing with a load testing tool called &lt;a href="http://blitz.io" target="_blank"&gt;Blitz&lt;/a&gt;. You can create a free account which provides you the ability to ‘rush’ your web application with 250 concurrent users (or less) for 1 minute. And of course, you can increase both the number of concurrent users as well as the duration of the load tests after you specify a credit card number. &lt;/p&gt; &lt;p align="justify"&gt;The interface is pretty slick, as it provides you with a kind of command line interface.&amp;nbsp;&amp;nbsp; &lt;/p&gt; &lt;p align="justify"&gt;&lt;a href="http://lh5.ggpht.com/-4-Nm2J4uKN8/UAm_teRim-I/AAAAAAAABrY/7qp2fXjj16Y/s1600-h/image%25255B15%25255D.png"&gt;&lt;img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://lh5.ggpht.com/-7sYLJ_6-lxQ/UAm_uHgjOuI/AAAAAAAABrc/K6x9zjdEq7o/image_thumb%25255B11%25255D.png?imgmax=800" width="445" height="102"&gt;&lt;/a&gt;&lt;/p&gt; &lt;p align="justify"&gt;Here I specified to run a load test, increasing the number of concurrent users from 1 to 12 over a period of one minute. The test is going to run from their datacenter in Ireland. You can choose from a couple of places in the US as well. &lt;/p&gt; &lt;p align="justify"&gt;This command yields the following results.&lt;/p&gt; &lt;p align="justify"&gt;&lt;a href="http://lh5.ggpht.com/-tNSH3egI7uI/UAm_u-v4GtI/AAAAAAAABro/uFqS886Xipw/s1600-h/image%25255B14%25255D.png"&gt;&lt;img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://lh3.ggpht.com/-Qt0zB6E0IX4/UAm_wR5EzaI/AAAAAAAABrw/3_7jsetBqN0/image_thumb%25255B10%25255D.png?imgmax=800" width="588" height="493"&gt;&lt;/a&gt;&lt;/p&gt; &lt;p&gt;Blitz also integrates with web application performance tools like &lt;a href="http://newrelic.com/" target="_blank"&gt;New Relic&lt;/a&gt; which enables you to further analyze the results and see what’s going on in your web application.&lt;/p&gt; &lt;p&gt;This is actually the first time ever that I had so much fun doing load tests &lt;img style="border-bottom-style: none; border-left-style: none; border-top-style: none; border-right-style: none" class="wlEmoticon wlEmoticon-winkingsmile" alt="Winking smile" src="http://lh3.ggpht.com/-IwREhMLivEk/UAm_xfcc_eI/AAAAAAAABr0/XKMkfHyIiTA/wlEmoticon-winkingsmile%25255B2%25255D.png?imgmax=800"&gt;.&lt;/p&gt; &lt;p&gt;Until next time.&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/ShareTheIntellectualWealth/~4/4-Bjf6K3-ME" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/ShareTheIntellectualWealth/~3/4-Bjf6K3-ME/blitz.html</link><author>noreply@blogger.com (Jan Van Ryswyck)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://lh5.ggpht.com/-7sYLJ_6-lxQ/UAm_uHgjOuI/AAAAAAAABrc/K6x9zjdEq7o/s72-c/image_thumb%25255B11%25255D.png?imgmax=800" height="72" width="72" /><thr:total>0</thr:total><feedburner:origLink>http://vanryswyckjan.blogspot.com/2012/07/blitz.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-15643421.post-7052950019492986819</guid><pubDate>Tue, 17 Jul 2012 10:00:00 +0000</pubDate><atom:updated>2012-07-17T12:00:09.531+02:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Books</category><title>Outliers – The Story of Success</title><description>&lt;p&gt;&lt;a href="http://www.amazon.com/exec/obidos/ASIN/0316017930/elegantcode-20" target="_blank"&gt;&lt;img style="margin: 0px auto; display: block; float: none" alt="A single marble is in the center, while a group of marbles is at the top." src="http://upload.wikimedia.org/wikipedia/en/thumb/b/be/Outliers.png/200px-Outliers.png" width="201" height="306"&gt;&lt;/a&gt;&lt;/p&gt; &lt;p align="justify"&gt;A couple of weeks ago, I digested the audio version of &lt;a href="http://www.amazon.com/exec/obidos/ASIN/0316017930/elegantcode-20" target="_blank"&gt;Outliers – The Story of Success&lt;/a&gt;, written by &lt;a href="http://en.wikipedia.org/wiki/Malcolm_Gladwell" target="_blank"&gt;Malcolm Gladwell&lt;/a&gt;. In this book, the author tells the story of a couple well-known and also lesser-known individuals that are considered to be successful. But what’s particularly interesting about this book, is the analysis the author makes to uncover the exact reason(s) that make these talented people stand out from the masses. &lt;/p&gt; &lt;p align="justify"&gt;There seem to be several factors at play. Apparently, being talented isn’t enough on its own (duh). Getting opportunities and taking them, hard work (the famous &lt;a href="http://www.gladwell.com/outliers/outliers_excerpt1.html" target="_blank"&gt;10.000 hour rule&lt;/a&gt;) and perseverance are just a couple of reasons that pop up regularly throughout the book. But more stunningly are reasons like the month of the year that some of the people discussed are born to even the particular year itself. The author claims that this is the very reason why early geeks like Bill Gates, Steve Jobs, Paul Allen, Bill Joy, etc. … were so successful.&amp;nbsp; The very year that these guys were born turned out to be a major part of their success.&lt;/p&gt; &lt;p align="justify"&gt;Interested? Check out &lt;a href="http://en.wikipedia.org/wiki/Outliers_(book)" target="_blank"&gt;this page on Wikipedia&lt;/a&gt; that is devoted to this fascinating book. Make sure to pick up a copy. You won’t regret it!&lt;/p&gt; &lt;p align="justify"&gt;Until next time.&amp;nbsp; &lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/ShareTheIntellectualWealth/~4/iLPQw1RoWGg" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/ShareTheIntellectualWealth/~3/iLPQw1RoWGg/outliers-story-of-success.html</link><author>noreply@blogger.com (Jan Van Ryswyck)</author><thr:total>0</thr:total><feedburner:origLink>http://vanryswyckjan.blogspot.com/2012/07/outliers-story-of-success.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-15643421.post-3403715064510791020</guid><pubDate>Tue, 03 Jul 2012 10:00:00 +0000</pubDate><atom:updated>2012-07-25T20:41:53.015+02:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Core Skills</category><title>My Developers Life–The Social Media Diet</title><description>&lt;p align="justify"&gt;In the previous blog posts I discussed the importance of getting enough sleep and physical exercise. For this post I want to provide a quick shout out of the social media diet that I’m currently trying out. &lt;/p&gt; &lt;p align="justify"&gt;My name is Jan and I don’t have a Facebook or a Netlog account of some kind. I do have a Google+ account that I haven’t visited in like four months. I also have a Twitter account that I’ve used quite often in the past. Currently I’m checking my Twitter account no more than two times a day for only a couple of minutes. Quite often I don’t visit it at all. Why? Because I let it all go.&lt;/p&gt; &lt;p align="justify"&gt;I did not gain as much free time as I initially anticipated. But what I did gain was my ability to focus on stuff that I kept postponing for some time and more efficiency while doing it. Looking back, my brain feels less flooded and I no longer have that nagging urge that I’m missing out on information that is not really that important in the first place.&amp;nbsp;&amp;nbsp; &lt;/p&gt; &lt;p align="justify"&gt;Don’t get me wrong here. I’m not entirely condemning social media either. I do get some value out of it, at the very least some entertainment only when I’m open for it. But I just let myself take some benefit from social media in the most superficial way as I possible can without getting too much involved. That way I’m able to pick up or learn something new without feeling like an informationholic. &lt;/p&gt; &lt;p align="justify"&gt;I no longer have a Twitter client constantly running in the background. I also disabled all other kinds of notifications popping up like e-mail, … etc. And I must say that it works like a charm.&lt;/p&gt; &lt;p align="justify"&gt;Close your browser or social media client and get out there! Exercise. Read a book. Learn a new programming language. Play with your kids. Listen to some music. Garden. Anything. No one on his dead bed ever said, “I wish I had wasted more time using social media”. Don’t be afraid to miss out on that one tweet or message, because you’re probably already lost track of what’s really important.&lt;/p&gt; &lt;p align="justify"&gt;Until next time.&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/ShareTheIntellectualWealth/~4/sGXa19s3O5w" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/ShareTheIntellectualWealth/~3/sGXa19s3O5w/my-developers-livethe-social-media-diet.html</link><author>noreply@blogger.com (Jan Van Ryswyck)</author><thr:total>0</thr:total><feedburner:origLink>http://vanryswyckjan.blogspot.com/2012/07/my-developers-livethe-social-media-diet.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-15643421.post-1602039258388646576</guid><pubDate>Tue, 12 Jun 2012 10:00:00 +0000</pubDate><atom:updated>2012-06-12T22:17:01.444+02:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Core Skills</category><title>My Developers Life - The Importance of Physical Exercise</title><description>&lt;p align="justify"&gt;In the previous blog post, I discussed the importance of getting enough sleep every single night. For this post I want to emphasize the importance of physical exercise. I’m writing these words after having run approximately 15 kilometers. This I do three times a week, every week, throughout the whole year with only a few exceptions. And the reason for this is pretty obvious. Since I started running back in 2007, I never felt better.&lt;/p&gt; &lt;p align="justify"&gt;As software developers, we basically sit at a desk whole day churning out code. The only form of physical movement we get is when we drag our bodies to the coffee machine, the water cooler or the vending machine when we want to get a snack. This means that we don’t get to burn as much calories as other people who have professions that involve more physical labor. For some of us, this is one of the many reasons why we chose this profession in the first place. I know I did ;-). Unfortunately, our text editor skills don’t cause us to burn any calories. What a bummer!&lt;/p&gt; &lt;p align="justify"&gt;During my childhood, I practiced several sports like soccer and judo. But when I started going to graduate school this all stopped. I slowly gained weight over the years. I didn’t have any physical exercise for about fifteen years. But at some point, around September 2007, I just bought myself some running shoes and started running short distances twice a week. Since then, I stopped making excuses for myself and improved my physical shape and my overall health. I learned so many valuable lessons, which led me to write this blog post.&lt;/p&gt; &lt;p align="justify"&gt;In hindsight, I can’t even remember what the actual tipping point was for me. What I do know is that this has been one of the best decisions I have ever made in my life. How do I keep this up? Quite frankly, this has always been the easy part. I started out by reserving 20 minutes in my agenda twice a week. I made some decent arrangements with my spouse and family about how and when. After settling on the best time frame, I treated these “appointments with myself” as non-negotiable. Now I reserve 1 hour and thirty minutes three times a week. I try to schedule all other appointments around these blocks of time. Again, have a good talk with your family and try to come up with the best possible time frame. I can’t emphasize this enough. &lt;/p&gt; &lt;p align="justify"&gt;I can only encourage everyone who spends his days behind a desk to get some physical exercise at least every two days. Try choosing a sport that you actually like but also make sure that there is a low barrier of entry. This way you can get up and running in not time, and I mean that quite literally :-). You can also start out with some small and simple habits like taking the bike instead of the car for short distances, or taking the stairs instead of the elevator, etc. … and pick up on some more physical exercise along the way.&lt;/p&gt; &lt;p align="justify"&gt;A professional software developer not only takes care of writing good code, but also takes care of himself.&lt;/p&gt; &lt;p align="justify"&gt;Until next time and happy exercising!&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/ShareTheIntellectualWealth/~4/6oo5WOHzcDw" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/ShareTheIntellectualWealth/~3/6oo5WOHzcDw/my-developers-life-importance-of.html</link><author>noreply@blogger.com (Jan Van Ryswyck)</author><thr:total>0</thr:total><feedburner:origLink>http://vanryswyckjan.blogspot.com/2012/06/my-developers-life-importance-of.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-15643421.post-9139976517580215682</guid><pubDate>Tue, 29 May 2012 10:00:00 +0000</pubDate><atom:updated>2012-05-29T12:00:06.102+02:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Core Skills</category><title>My Developers Life - The Importance of Sleep</title><description>&lt;p align="justify"&gt;When I was reading &lt;a href="http://vanryswyckjan.blogspot.com/2012/02/book-review-just-for-fun.html" target="_blank"&gt;Just for Fun&lt;/a&gt;, I came across this paragraph in the book where &lt;a href="http://en.wikipedia.org/wiki/Linus_Torvalds" target="_blank"&gt;Linus Torvalds&lt;/a&gt; stated that he never missed a good night’s sleep during the early days when he was hacking together the first version of the Linux kernel. In fact, he clearly stated in his book that he doesn’t allow his pet project to come between him and a minimum of eight hours of sleep each night. &lt;/p&gt; &lt;p align="justify"&gt;Let’s think about this for a moment. &lt;/p&gt; &lt;p align="justify"&gt;Here’s this guy who wrote an insane amount of complex code without pulling all night death marches. After I read that passage, I couldn’t help but reflect on my own behavior. I’ve been working on countless little pet projects over the past couple of years, often churning out code until 3 or 4 o’clock in the morning during the weekends or holidays. This made me realize that I needed to do something about it. And you know what the stupid part was? I had the right habits all along!&amp;nbsp; &lt;/p&gt; &lt;p align="justify"&gt;Those who know me know that I start my working days quite early. I usually arrive at the office around 7 o’clock in the morning, getting a fresh and quiet head start of my day. This implies that I go to sleep very early as well on working days, trying to get as much sleep as I possibly can every night.&lt;/p&gt; &lt;p align="justify"&gt;So, now that I realized that I already had the right behaviors in place, the only thing that I needed to do was to extend this behavior to the weekends and holidays. And I must say that it works great! I instantly stopped hacking on code during the night and now I go to sleep early every day. I also get up early every single day which makes me feel more productive, more efficient and definitely more focused. After doing this for a couple of weeks, I could only come to the conclusion that I felt much better overall.&lt;/p&gt; &lt;p align="justify"&gt;However, I’m still not entirely there yet. There are some nights that I don’t get to have a full eight hours of sleep, mostly on working days. This is something that I still need to work on, so I create the right habits in order to achieve this goal.&amp;nbsp; &lt;/p&gt; &lt;p align="justify"&gt;No more sleep deprivation for me. Everyone knows that working more than 8 hours a day is madness. Well, the same goes for writing code after midnight. I came to realize that a good night’s sleep is probably one of the most important tools you have as a developer. A well rested brain is more useful than a tired and a sleepy one.&lt;/p&gt; &lt;p align="justify"&gt;I encourage you to try this for two weeks and reflect on your mood, productivity and your general well being.&lt;/p&gt; &lt;p align="justify"&gt;Until next time and good night! &lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/ShareTheIntellectualWealth/~4/43yAoYzw-bs" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/ShareTheIntellectualWealth/~3/43yAoYzw-bs/my-developers-life-importance-of-sleep.html</link><author>noreply@blogger.com (Jan Van Ryswyck)</author><thr:total>5</thr:total><feedburner:origLink>http://vanryswyckjan.blogspot.com/2012/05/my-developers-life-importance-of-sleep.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-15643421.post-4992266623437570101</guid><pubDate>Tue, 15 May 2012 10:00:00 +0000</pubDate><atom:updated>2012-05-15T12:00:05.503+02:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Node.js</category><title>Taking Toddler Steps with Node.js – Passport</title><description>&lt;p align="justify"&gt;Recently I added Twitter authentication to &lt;a href="http://vanryswyckjan.blogspot.com/2012/01/introducing-trackmyrun.html" target="_blank"&gt;TrackMyRun&lt;/a&gt; using a library called &lt;a href="http://passportjs.org/" target="_blank"&gt;Passport&lt;/a&gt;. I was pretty impressed how smooth this all went as I completely neglected all security concerns from the get go, which is definitely not recommended by the way. For this post I’ll walk you through the process of setting up Passport for &lt;a href="http://expressjs.com/" target="_blank"&gt;Express&lt;/a&gt; using Twitter OAuth authentication.&lt;/p&gt; &lt;p align="justify"&gt;&lt;a href="https://github.com/jaredhanson/passport" target="_blank"&gt;Passport&lt;/a&gt; is actually the core library which provides support for OpenId and OAuth authentication. Instead of being one single monolithic library, Passport uses strategies that support authentication directly with specific &lt;a href="http://passportjs.org/guide/openid-providers.html" target="_blank"&gt;OpenId&lt;/a&gt;/&lt;a href="http://passportjs.org/guide/oauth-providers.html" target="_blank"&gt;OAuth&lt;/a&gt; providers. &lt;/p&gt; &lt;p align="justify"&gt;So in order to get up and running, we need to install &lt;a href="https://github.com/jaredhanson/passport" target="_blank"&gt;passport&lt;/a&gt; as well as &lt;a href="https://github.com/jaredhanson/passport-twitter" target="_blank"&gt;passport-twitter&lt;/a&gt; for Twitter OAuth authentication. After we install these modules using npm, we can start by configuring the Twitter strategy.&lt;/p&gt;&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;var&lt;/span&gt; express = require(&lt;span class="str"&gt;'express'&lt;/span&gt;),
    passport = require(&lt;span class="str"&gt;'passport'&lt;/span&gt;),
    TwitterStrategy = require(&lt;span class="str"&gt;'passport-twitter'&lt;/span&gt;).Strategy;

&lt;span class="kwrd"&gt;var&lt;/span&gt; users = [];

passport.use(&lt;span class="kwrd"&gt;new&lt;/span&gt; TwitterStrategy({
        consumerKey: &lt;span class="str"&gt;'twitter-app-consumer-key'&lt;/span&gt;,
        consumerSecret: &lt;span class="str"&gt;'twitter-app-consumer-secret'&lt;/span&gt;,
        callbackURL: &lt;span class="str"&gt;"http://test.passport-twitter.com:3000/auth/twitter/callback"&lt;/span&gt;
    },
    &lt;span class="kwrd"&gt;function&lt;/span&gt;(token, tokenSecret, profile, done) {
        &lt;span class="kwrd"&gt;var&lt;/span&gt; user = users[profile.id] || 
                   (users[profile.id] = { id: profile.id, name: profile.username });
        done(&lt;span class="kwrd"&gt;null&lt;/span&gt;, user);
    }
));&lt;/pre&gt;
&lt;style type="text/css"&gt;.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
&lt;/style&gt;

&lt;p align="justify"&gt;The strategy must be configured by providing the consumer key and consumer secret as well as the callback URL. I’m not going too much in depth on how OAuth works. Make sure to check out the &lt;a href="https://dev.twitter.com/" target="_blank"&gt;Twitter for developers&lt;/a&gt; website on how to configure an application that uses the Twitter API. &lt;/p&gt;
&lt;p align="justify"&gt;Besides adding the strategy for Twitter, we also specified a callback function. In this callback, we’re supposed to find and verify a user that matches a specified set of credentials. Usually we have some code here that checks to see if the specified user exists in a database of some sort. In order not to clutter this example, I used a simple array here instead. &lt;/p&gt;
&lt;p align="justify"&gt;If we can find the requested user in our data store, we need to invoke &lt;em&gt;done()&lt;/em&gt; to supply the Passport with the user.&lt;/p&gt;&lt;pre class="csharpcode"&gt;done(&lt;span class="kwrd"&gt;null&lt;/span&gt;, user);&lt;/pre&gt;
&lt;p&gt;When the user cannot be found, we can simply pass &lt;em&gt;false&lt;/em&gt; instead of a user object.&lt;/p&gt;&lt;pre class="csharpcode"&gt;done(&lt;span class="kwrd"&gt;null&lt;/span&gt;, &lt;span class="kwrd"&gt;false&lt;/span&gt;);&lt;/pre&gt;
&lt;style type="text/css"&gt;.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
&lt;/style&gt;

&lt;p align="justify"&gt;In our example we always ensure that the specified credentials match a particular user object. Next we need to configure the Passport middleware for initialization and session management.&lt;/p&gt;&lt;pre class="csharpcode"&gt;application.configure(&lt;span class="kwrd"&gt;function&lt;/span&gt;() {
    application.use(express.bodyParser());
    application.use(express.methodOverride());
    application.use(express.cookieParser());
    application.use(express.session( { secret: &lt;span class="str"&gt;'498f99f3bbee4ae3a075eada02488464'&lt;/span&gt; } ));
    application.use(passport.initialize());
    application.use(passport.session());
    application.use(application.router);
    application.use(express.errorHandler({ showStack: &lt;span class="kwrd"&gt;true&lt;/span&gt;, dumpExceptions: &lt;span class="kwrd"&gt;true&lt;/span&gt; }));
    application.set(&lt;span class="str"&gt;'view engine'&lt;/span&gt;, &lt;span class="str"&gt;'jade'&lt;/span&gt;);
});&lt;/pre&gt;
&lt;p&gt;
&lt;style type="text/css"&gt;.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
&lt;/style&gt;
&lt;/p&gt;
&lt;p align="justify"&gt;Please note that the &lt;em&gt;express.session()&lt;/em&gt; middleware needs be called before &lt;em&gt;passport.session()&lt;/em&gt;. Next we add the routes necessary for authenticating requests and handling the token callback. &lt;/p&gt;&lt;pre class="csharpcode"&gt;application.get(&lt;span class="str"&gt;'/auth/twitter'&lt;/span&gt;, passport.authenticate(&lt;span class="str"&gt;'twitter'&lt;/span&gt;));

application.get(&lt;span class="str"&gt;'/auth/twitter/callback'&lt;/span&gt;, 
    passport.authenticate(&lt;span class="str"&gt;'twitter'&lt;/span&gt;, 
        { successRedirect: &lt;span class="str"&gt;'/'&lt;/span&gt;, 
          failureRedirect: &lt;span class="str"&gt;'/auth/twitter'&lt;/span&gt; }));&lt;/pre&gt;
&lt;style type="text/css"&gt;.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
&lt;/style&gt;

&lt;p align="justify"&gt;Last but not least we also need to declare a &lt;em&gt;serializeUser/deserializeUser&lt;/em&gt; callback function. These are necessary for supporting login sessions. &lt;/p&gt;&lt;pre class="csharpcode"&gt;passport.serializeUser(&lt;span class="kwrd"&gt;function&lt;/span&gt;(user, done) {
    done(&lt;span class="kwrd"&gt;null&lt;/span&gt;, user.id);
});

passport.deserializeUser(&lt;span class="kwrd"&gt;function&lt;/span&gt;(id, done) {
    &lt;span class="kwrd"&gt;var&lt;/span&gt; user = users[id];
    done(&lt;span class="kwrd"&gt;null&lt;/span&gt;, user);
});&lt;/pre&gt;
&lt;p&gt;
&lt;style type="text/css"&gt;.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
&lt;/style&gt;
&lt;/p&gt;
&lt;p align="justify"&gt;Instead of reading the requested user objects from the data store, we simply use the array that we incorporated earlier.&lt;/p&gt;
&lt;p align="justify"&gt;That’s basically the thing. We can add other authentication providers by simply configuring more strategies. Have a look at the &lt;a href="https://github.com/JanVanRyswyck/node-examples/tree/master/passport" target="_blank"&gt;full source code&lt;/a&gt; of this example and try to get it up and running.&lt;/p&gt;
&lt;p&gt;Until next time.&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/ShareTheIntellectualWealth/~4/ZTBfHzbuXks" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/ShareTheIntellectualWealth/~3/ZTBfHzbuXks/taking-toddler-steps-with-nodejs.html</link><author>noreply@blogger.com (Jan Van Ryswyck)</author><thr:total>0</thr:total><feedburner:origLink>http://vanryswyckjan.blogspot.com/2012/05/taking-toddler-steps-with-nodejs.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-15643421.post-8795036551852427119</guid><pubDate>Fri, 06 Apr 2012 20:35:00 +0000</pubDate><atom:updated>2012-04-06T22:35:04.858+02:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Node.js</category><title>Taking Toddler Steps with Node.js - Express Error Handling</title><description>&lt;p align="justify"&gt;In the previous post I wrote about &lt;a href="http://vanryswyckjan.blogspot.com/2012/01/taking-toddler-steps-with-nodejs.html" target="_blank"&gt;my personal routing flavor&lt;/a&gt; for &lt;a href="http://expressjs.com" target="_blank"&gt;Express&lt;/a&gt;. For this post, I want to briefly discuss how to set up error handling using Express.&lt;/p&gt; &lt;p align="justify"&gt;In order to get up and going very quickly, we only need to add the &lt;em&gt;&lt;a href="http://www.senchalabs.org/connect/errorHandler.html" target="_blank"&gt;errorHandler&lt;/a&gt;&lt;/em&gt; middleware provided by &lt;a href="https://github.com/senchalabs/connect" target="_blank"&gt;Connect&lt;/a&gt;.&lt;/p&gt;&lt;pre style="width: 100%; height: 23px" class="csharpcode"&gt;application.use(express.errorHandler({ showStack: &lt;span class="kwrd"&gt;true&lt;/span&gt;, dumpExceptions: &lt;span class="kwrd"&gt;true&lt;/span&gt; }));&lt;/pre&gt;
&lt;p align="justify"&gt;
&lt;style type="text/css"&gt;.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
&lt;/style&gt;
Here we configured the &lt;em&gt;errorHandler&lt;/em&gt; middleware to report on exceptions and show the stack trace as well. This is quite handy during development as this setup provides us with enough detail.&lt;/p&gt;
&lt;p align="justify"&gt;&lt;a href="http://lh5.ggpht.com/-NvNtFrF0UmQ/T39TaDxVfyI/AAAAAAAABps/X-szL82RZmI/s1600-h/image4.png"&gt;&lt;img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://lh3.ggpht.com/-mEnyiZEJr7E/T39TcJ7LliI/AAAAAAAABp0/d4hMgPPBHE4/image_thumb2.png?imgmax=800" width="586" height="320"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p align="justify"&gt;But this is not very effective when we want to move our application to a production environment. When deployed into production, we usually want to show a user-friendly message instead of technical details, stack traces and what not. In this case we can use the &lt;em&gt;application.error()&lt;/em&gt; method. This function receives all errors thrown by the regular route functions or errors passed to the &lt;em&gt;next()&lt;/em&gt; function. In this &lt;em&gt;catch-all-errors&lt;/em&gt; function we can simply render our own custom view.&lt;/p&gt;&lt;pre style="width: 100%; height: 126px" class="csharpcode"&gt;application.error(&lt;span class="kwrd"&gt;function&lt;/span&gt;(error, request, response, next) {
    response.render(&lt;span class="str"&gt;'500'&lt;/span&gt;, {
        status: 500,
        error: util.inspect(error),
        showDetails: application.settings.showErrorDetails
    });
});&lt;/pre&gt;
&lt;style type="text/css"&gt;.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
&lt;/style&gt;

&lt;p align="justify"&gt;This is how our custom error page looks like:&lt;/p&gt;
&lt;p align="justify"&gt;&lt;a href="http://lh5.ggpht.com/-RgWQUBcfvUc/T39TdKiV-aI/AAAAAAAABp8/tnT7Y-ViCrQ/s1600-h/image9.png"&gt;&lt;img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://lh5.ggpht.com/-QSuY-HQ9194/T39Td8i6kwI/AAAAAAAABqA/IpDWTZ0avSo/image_thumb5.png?imgmax=800" width="572" height="205"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p align="justify"&gt;We can also use the &lt;em&gt;application.error()&lt;/em&gt; function for rendering custom pages for all kinds of specific errors. Suppose we want to render a custom page for ‘404 – Page Not Found’ errors. Quite easy. We just need to register a catch-all route &lt;em&gt;&lt;strong&gt;after&lt;/strong&gt;&lt;/em&gt; all the regular route functions that simply throws a custom error.&lt;/p&gt;&lt;pre style="width: 100%; height: 208px" class="csharpcode"&gt;&lt;span class="kwrd"&gt;function&lt;/span&gt; PageNotFoundError(message){
  &lt;span class="kwrd"&gt;this&lt;/span&gt;.name = &lt;span class="str"&gt;'PageNotFoundError'&lt;/span&gt;;
  Error.call(&lt;span class="kwrd"&gt;this&lt;/span&gt;, message);
  Error.captureStackTrace(&lt;span class="kwrd"&gt;this&lt;/span&gt;, arguments.callee);
}

PageNotFoundError.prototype.__proto__ = Error.prototype;

application.use(&lt;span class="kwrd"&gt;function&lt;/span&gt;(request, response, next) {
    next(&lt;span class="kwrd"&gt;new&lt;/span&gt; PageNotFoundError())        
});&lt;/pre&gt;
&lt;p align="justify"&gt;
&lt;style type="text/css"&gt;.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
&lt;/style&gt;
Next we need to enhance &lt;em&gt;application.error()&lt;/em&gt; function so that it appropriately handles our &lt;em&gt;PageNotFoundError&lt;/em&gt;.&lt;/p&gt;&lt;pre style="width: 100%; height: 255px" class="csharpcode"&gt;application.error(&lt;span class="kwrd"&gt;function&lt;/span&gt;(error, request, response, next) {
    &lt;span class="kwrd"&gt;if&lt;/span&gt; (&lt;span class="kwrd"&gt;typeof&lt;/span&gt; error === &lt;span class="kwrd"&gt;typeof&lt;/span&gt; PageNotFoundError) {
      response.render(&lt;span class="str"&gt;'404'&lt;/span&gt;, {
        status: 404
      });
    } 
    &lt;span class="kwrd"&gt;else&lt;/span&gt; {
        response.render(&lt;span class="str"&gt;'500'&lt;/span&gt;, {
            status: 500,
            error: util.inspect(error),
            showDetails: application.settings.showErrorDetails
        });
    }
});&lt;/pre&gt;
&lt;style type="text/css"&gt;.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
&lt;/style&gt;

&lt;p align="justify"&gt;As you can see, error handling is quite easy to setup for different environments using Express.&lt;/p&gt;
&lt;p align="justify"&gt;Until next time.&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/ShareTheIntellectualWealth/~4/2orelpjbikQ" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/ShareTheIntellectualWealth/~3/2orelpjbikQ/taking-toddler-steps-with-nodejs.html</link><author>noreply@blogger.com (Jan Van Ryswyck)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://lh3.ggpht.com/-mEnyiZEJr7E/T39TcJ7LliI/AAAAAAAABp0/d4hMgPPBHE4/s72-c/image_thumb2.png?imgmax=800" height="72" width="72" /><thr:total>0</thr:total><feedburner:origLink>http://vanryswyckjan.blogspot.com/2012/04/taking-toddler-steps-with-nodejs.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-15643421.post-2944718686332075635</guid><pubDate>Tue, 27 Mar 2012 11:00:00 +0000</pubDate><atom:updated>2012-03-27T13:00:08.452+02:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Node.js</category><title>Taking Toddler Steps with Node.js - Thou Shalt Not Deny Asynchrony</title><description>&lt;p align="justify"&gt;A few of weeks ago, I ran into this awesome article &lt;a href="http://howtonode.org/understanding-process-next-tick" target="_blank"&gt;“Understanding process.nextTick()”&lt;/a&gt; on the &lt;a href="http://howtonode.org" target="_blank"&gt;How To Node&lt;/a&gt; blog. In this article, the ever friendly &lt;a href="http://kishorelive.com/" target="_blank"&gt;Kishore Nallan&lt;/a&gt; shows a couple of common use cases for the &lt;a href="http://nodejs.org/docs/latest/api/process.html#process_process_nexttick_callback" target="_blank"&gt;process.nextTick()&lt;/a&gt; function of the core Node.js library. You should read this blog post three times, at the very least.&lt;/p&gt; &lt;p align="justify"&gt;What I found to be particularly interesting at the time was the part on how to keep callbacks truly asynchronous. I admit that this is something that I’ve been struggling with for a while. At first I thought this was not a big deal, but this can become quite troublesome in your applications if you’re not careful. &lt;/p&gt; &lt;p align="justify"&gt;Let’s look at some code.&lt;/p&gt; &lt;style type="text/css"&gt;.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
&lt;/style&gt; &lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;var&lt;/span&gt; fileSystem = require(&lt;span class="str"&gt;'fs'&lt;/span&gt;);

module.exports = (&lt;span class="kwrd"&gt;function&lt;/span&gt;() {
    &lt;span class="kwrd"&gt;function&lt;/span&gt; Configuration() {}
    Configuration.settings = &lt;span class="kwrd"&gt;null&lt;/span&gt;;

    Configuration.getSettings = &lt;span class="kwrd"&gt;function&lt;/span&gt;(callback) {
        &lt;span class="kwrd"&gt;var&lt;/span&gt; self = &lt;span class="kwrd"&gt;this&lt;/span&gt;;
        &lt;span class="kwrd"&gt;if&lt;/span&gt;(&lt;span class="kwrd"&gt;this&lt;/span&gt;.settings) {
            &lt;span class="kwrd"&gt;return&lt;/span&gt; callback(&lt;span class="kwrd"&gt;null&lt;/span&gt;, self.settings);
        }

        fileSystem.readFile(__dirname + &lt;span class="str"&gt;'/config.json'&lt;/span&gt;, &lt;span class="str"&gt;'utf8'&lt;/span&gt;, 
            &lt;span class="kwrd"&gt;function&lt;/span&gt;(error, data) {
                &lt;span class="kwrd"&gt;var&lt;/span&gt; self = &lt;span class="kwrd"&gt;this&lt;/span&gt;;
                &lt;span class="kwrd"&gt;if&lt;/span&gt;(error) {
                    &lt;span class="kwrd"&gt;return&lt;/span&gt; callback(error);
                }

                &lt;span class="kwrd"&gt;this&lt;/span&gt;.settings = JSON.parse(data);
                &lt;span class="kwrd"&gt;return&lt;/span&gt; callback(&lt;span class="kwrd"&gt;null&lt;/span&gt;, self.settings);
        });
    };

    &lt;span class="kwrd"&gt;return&lt;/span&gt; Configuration;
})();
&lt;/pre&gt;
&lt;style type="text/css"&gt;.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
&lt;/style&gt;

&lt;p align="justify"&gt;Here we’re looking at a simple module that exposes a single function named &lt;em&gt;getSettings&lt;/em&gt;. When called for the first time, this function reads the content of a JSON file and stores the result in a &lt;a href="http://vanryswyckjan.blogspot.com/2011/01/basic-javascript-part-7-static.html" target="_blank"&gt;static property&lt;/a&gt;. For all successive function calls, we simply serve the object stored in the static property without reading the JSON file again. This all looks very simple and everything works great. But this code is also highly inconsistent!&lt;/p&gt;
&lt;p align="justify"&gt;Let’s have a closer look.&lt;/p&gt;
&lt;p align="justify"&gt;When reading the content of the JSON file, we invoke the callback, specified as a parameter to &lt;em&gt;getSettings&lt;/em&gt;, inside another callback (the anonymous function specified to the &lt;em&gt;readFile&lt;/em&gt; function). This means that the invocation of the specified callback is asynchronous because the callback of the &lt;em&gt;readFile&lt;/em&gt; function is also asynchronous. Nothing wrong here.&lt;/p&gt;
&lt;p align="justify"&gt;When calling the &lt;em&gt;getSettings&lt;/em&gt; function a second time, we instantly invoke the specified callback with the object stored in the static variable. This means that the callback is executed synchronously. From the perspective of the client of our module, this is inconsistent behavior. The callback specified by the client code is executed asynchronously on the first call while it is executed synchronously on all successive calls. &lt;/p&gt;
&lt;p align="justify"&gt;In order to fix this inconsistent behavior, we can use the &lt;em&gt;process.nextTick()&lt;/em&gt; function which defers the execution of our callback until the next iteration of the event loop. &lt;/p&gt;
&lt;p align="justify"&gt;Let’s have a look at the code that fixes this issue:&lt;/p&gt;&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;if&lt;/span&gt;(&lt;span class="kwrd"&gt;this&lt;/span&gt;.settings) {
    &lt;span class="kwrd"&gt;return&lt;/span&gt; process.nextTick(&lt;span class="kwrd"&gt;function&lt;/span&gt;() {
        &lt;span class="kwrd"&gt;return&lt;/span&gt; callback(&lt;span class="kwrd"&gt;null&lt;/span&gt;, self.settings);
    });
}
&lt;/pre&gt;
&lt;style type="text/css"&gt;.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
&lt;/style&gt;

&lt;p align="justify"&gt;This is something that you need to think about when invoking callback functions.&amp;nbsp; &lt;/p&gt;
&lt;p align="justify"&gt;Until next time.&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/ShareTheIntellectualWealth/~4/bjzB92vnhuo" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/ShareTheIntellectualWealth/~3/bjzB92vnhuo/taking-toddler-steps-with-nodejs-thou.html</link><author>noreply@blogger.com (Jan Van Ryswyck)</author><thr:total>0</thr:total><feedburner:origLink>http://vanryswyckjan.blogspot.com/2012/03/taking-toddler-steps-with-nodejs-thou.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-15643421.post-6072445492553194091</guid><pubDate>Fri, 23 Mar 2012 21:38:00 +0000</pubDate><atom:updated>2012-03-23T22:38:50.448+01:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Node.js</category><title>Taking Toddler Steps with Node.js - Npm Police</title><description>&lt;p align="justify"&gt;Earlier this week, I ran across this incredibly helpful utility called &lt;a href="https://github.com/pksunkara/npm-police" target="_blank"&gt;npm-police&lt;/a&gt;. It analyzes all packages that are specified in a package.json file and reports which packages need to be updated. It also leaves you with a couple of suggestions on how to improve the content of the analyzed package.json file.&lt;/p&gt; &lt;p align="justify"&gt;&lt;a href="http://lh5.ggpht.com/-cIQG6pK8eKU/T2ztZGDwrfI/AAAAAAAABpI/x8_-b6S_Z1g/s1600-h/image%25255B9%25255D.png"&gt;&lt;img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://lh4.ggpht.com/-qOONVzaqbP4/T2ztaH9_zPI/AAAAAAAABpQ/sBb62jU4ONE/image_thumb%25255B5%25255D.png?imgmax=800" width="600" height="155"&gt;&lt;/a&gt;&lt;/p&gt; &lt;p&gt;Another great tool in our toolbox.&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/ShareTheIntellectualWealth/~4/198ZtAP36UU" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/ShareTheIntellectualWealth/~3/198ZtAP36UU/taking-toddler-steps-with-nodejs-npm.html</link><author>noreply@blogger.com (Jan Van Ryswyck)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://lh4.ggpht.com/-qOONVzaqbP4/T2ztaH9_zPI/AAAAAAAABpQ/sBb62jU4ONE/s72-c/image_thumb%25255B5%25255D.png?imgmax=800" height="72" width="72" /><thr:total>0</thr:total><feedburner:origLink>http://vanryswyckjan.blogspot.com/2012/03/taking-toddler-steps-with-nodejs-npm.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-15643421.post-7796825925830373833</guid><pubDate>Tue, 28 Feb 2012 11:00:00 +0000</pubDate><atom:updated>2012-02-28T12:00:14.707+01:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Linux</category><category domain="http://www.blogger.com/atom/ns#">Books</category><title>Book Review: Just for Fun</title><description>&lt;p&gt;&lt;img style="margin: 0px 12px 0px 0px; display: inline; float: left" alt="Just for Fun: The Story of an Accidental Revolutionary" align="left" src="http://img2.imagesbn.com/images/102730000/102734498.jpg" width="237" height="357"&gt;&lt;/p&gt; &lt;p align="justify"&gt;A while ago, &lt;a href="https://twitter.com/RidingWolf" target="_blank"&gt;my colleague Wouter&lt;/a&gt; recommended this book titled &lt;a href="http://www.amazon.com/exec/obidos/ASIN/0066620732/elegantcode-20" target="_blank"&gt;Just for Fun – The Story of an Accidental Revolutionary&lt;/a&gt; written by &lt;a href="http://en.wikipedia.org/wiki/Linus_Torvalds" target="_blank"&gt;Linus Torvalds&lt;/a&gt;. The author needs no further introduction as he’s well known as the initiator of the &lt;a href="http://www.kernel.org/" target="_blank"&gt;Linux kernel&lt;/a&gt; and the most excellent &lt;a href="http://git-scm.com/" target="_blank"&gt;Git revision control system&lt;/a&gt;. I immediately ordered myself a copy of this book and finished reading it last weekend. I must admit that this was quite a fascinating read. The writing style is very entertaining and just plain witty at times. &lt;/p&gt; &lt;p align="justify"&gt;The book consists of three major parts. The first part, titled &lt;em&gt;“Birth of a Nerd”&lt;/em&gt;, describes the early life of the author in his home country Finland, how he got introduced to computers by his grandfather, etc. … . &lt;/p&gt; &lt;p align="justify"&gt;The second part, titled &lt;em&gt;“Birth of an Operating System”&lt;/em&gt;, is probably the most interesting part for us geeks. Here Linus describes how he started hacking on software while he was at university. At first he started working on a terminal emulator for &lt;a href="http://www.minix3.org/" target="_blank"&gt;Minix&lt;/a&gt; in order to learn about the x386 CPU instruction set for the PC he bought. This terminal emulator later grown into the first version of the Linux kernel after he implemented more and more Unix system functions. He further elaborates how these humble beginnings further evolved into the Linux operating system as we know it today. Truly an amazing story!&lt;/p&gt; &lt;p align="justify"&gt;The last part of the book, titled &lt;em&gt;“King of the ball”&lt;/em&gt;, discusses some varying topics like intellectual property, why open-source software makes sense and even the meaning of life. This section contains some very interesting perspectives regarding these topics.&amp;nbsp;&amp;nbsp; &lt;/p&gt; &lt;p align="justify"&gt;In conclusion, I very much liked reading this book. It’s a fast and easy read and you don’t even have to be a geek to make sense of this great story. This book is definitely obligatory reading for everyone who’s even slightly involved with open-source software. &lt;/p&gt; &lt;p align="justify"&gt;Two thumbs up!!&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/ShareTheIntellectualWealth/~4/HvNlPc3jgBM" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/ShareTheIntellectualWealth/~3/HvNlPc3jgBM/book-review-just-for-fun.html</link><author>noreply@blogger.com (Jan Van Ryswyck)</author><thr:total>0</thr:total><feedburner:origLink>http://vanryswyckjan.blogspot.com/2012/02/book-review-just-for-fun.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-15643421.post-6535290012558353124</guid><pubDate>Wed, 22 Feb 2012 11:00:00 +0000</pubDate><atom:updated>2012-02-22T12:00:11.012+01:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Linux</category><category domain="http://www.blogger.com/atom/ns#">Books</category><title>Book Review: The Unix Programming Environment</title><description>&lt;p&gt;&lt;a href="http://www.amazon.com/exec/obidos/ASIN/013937681X/elegantcode-20" target="_blank"&gt;&lt;img style="margin: 0px 10px 0px 0px; display: inline; float: left" alt="book cover" align="left" src="http://cm.bell-labs.com/cm/cs/upe/covers/english4.gif" width="221" height="293"&gt;&lt;/a&gt;&lt;/p&gt; &lt;p align="justify"&gt;A couple of weeks ago I finished reading &lt;a href="http://www.amazon.com/exec/obidos/ASIN/013937681X/elegantcode-20" target="_blank"&gt;The Unix Programming Environment&lt;/a&gt;, written by &lt;a href="http://en.wikipedia.org/wiki/Brian_Kernighan" target="_blank"&gt;Brian W. Kernighan&lt;/a&gt; and &lt;a href="http://en.wikipedia.org/wiki/Rob_Pike" target="_blank"&gt;Rob Pike&lt;/a&gt;. The main reason that I picked up this classic book was to learn more about the fundamentals of Unix. I must say that this book is an excellent guide to start learning more about this widespread operating system.&amp;nbsp; &lt;/p&gt; &lt;p align="justify"&gt;Ever since I started my programming career, I had this on and off relationship with Linux. There were times that I wanted to turn my back on Windows entirely and make the jump. Then there were also times that I couldn’t care less about Linux. But when I started playing with CouchDB and especially Node.js, this fascination for the other side has been growing steadily. In fact, I kind of made a small jump already. All of the code that I’ve developed during my spare time for the past 14 months have been hacked together on a Linux VM.&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/p&gt; &lt;p align="justify"&gt;I don’t want you to fall into the impression that Windows is a bad operating system either, quite on the contrary. I’ve been doing professional software development on Windows for more than 12 years now and I still believe that it’s a rock solid operating system. Why am I not&amp;nbsp; solely sticking to software development on Windows then? Well, for the simple reason that I’ve always had this nagging feeling that I’ve been missing out by not learning equally as much about Linux as I learned the past 12 years about Windows. So I kind of suspect that I’ll be running Windows in a VM on a Linux host in the not too distant future. I’m quite fascinated and determined to find my way in this mostly unknown environment.&lt;/p&gt; &lt;p align="justify"&gt;Anyway, back to the book. There are basically two major parts in this book. The first part deals with the basics, the file system, the shell and an introduction to &lt;a href="http://en.wikipedia.org/wiki/Sed" target="_blank"&gt;sed &amp;amp; awk&lt;/a&gt;. The second part discusses more advanced topics like shell programming, programming standard I/O and Unix system calls which I skipped entirely (for now). By completely focusing on the first part, I learned quite some stuff and also relearned a few things from the past. &lt;/p&gt; &lt;p align="justify"&gt;The only thing that bothered me about this book is that its quite boring. The book was originally published in 1984. Perhaps authors of technical books from back then were not supposed to have an entertaining writing style. I’m not sure. I was probably too busy playing with Legos at the time to notice ;-).&amp;nbsp; &lt;/p&gt; &lt;p align="justify"&gt;I now realize that I have quite a long journey ahead of me. Good thing that I started this path sooner than later.&lt;/p&gt; &lt;p align="justify"&gt;Until next time.&lt;/p&gt; &lt;p align="justify"&gt;PS: I recently switched from Shelfari to &lt;a href="http://www.goodreads.com/user/show/7085099-jan-van-ryswyck" target="_blank"&gt;GoodReads&lt;/a&gt; and I’m quite liking it so far. Just let me know if you have some interesting books to share. &lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/ShareTheIntellectualWealth/~4/tmH6-Bw6C5U" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/ShareTheIntellectualWealth/~3/tmH6-Bw6C5U/book-review-unix-programming.html</link><author>noreply@blogger.com (Jan Van Ryswyck)</author><thr:total>0</thr:total><feedburner:origLink>http://vanryswyckjan.blogspot.com/2012/02/book-review-unix-programming.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-15643421.post-3066122255512730176</guid><pubDate>Tue, 14 Feb 2012 11:00:00 +0000</pubDate><atom:updated>2012-02-14T12:00:11.387+01:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">CouchDB</category><title>View Collation in CouchDB</title><description>&lt;p align="justify"&gt;A couple of weeks ago I learned about a very neat feature of CouchDB called &lt;a href="http://wiki.apache.org/couchdb/View_collation" target="_blank"&gt;view collations&lt;/a&gt;. Basically, view collations enables us to make joins between documents. Let’s look at a simple example here.&lt;/p&gt; &lt;p align="justify"&gt;Suppose we have a &lt;em&gt;customer-order&lt;/em&gt; model where a customer can have one or more orders. The data of the documents for our example looks like this:&lt;/p&gt;&lt;pre style="width: 100%; height: 732px" class="csharpcode"&gt;{
   &lt;span class="str"&gt;"_id"&lt;/span&gt;: &lt;span class="str"&gt;"807023fe77d927e1c222adfe4a0007ba"&lt;/span&gt;,
   &lt;span class="str"&gt;"_rev"&lt;/span&gt;: &lt;span class="str"&gt;"2-2eae48beb08acc72ed2ab64ccfbcb1c7"&lt;/span&gt;,
   &lt;span class="str"&gt;"type"&lt;/span&gt;: &lt;span class="str"&gt;"customer"&lt;/span&gt;,
   &lt;span class="str"&gt;"firstName"&lt;/span&gt;: &lt;span class="str"&gt;"Homer"&lt;/span&gt;,
   &lt;span class="str"&gt;"lastName"&lt;/span&gt;: &lt;span class="str"&gt;"Simpson"&lt;/span&gt;
}

{
   &lt;span class="str"&gt;"_id"&lt;/span&gt;: &lt;span class="str"&gt;"807023fe77d927e1c222adfe4a0013c2"&lt;/span&gt;,
   &lt;span class="str"&gt;"_rev"&lt;/span&gt;: &lt;span class="str"&gt;"2-9fc81f3b250c514fbd7e1d5f4a44816b"&lt;/span&gt;,
   &lt;span class="str"&gt;"type"&lt;/span&gt;: &lt;span class="str"&gt;"order"&lt;/span&gt;,
   &lt;span class="str"&gt;"amount"&lt;/span&gt;: 34,
   &lt;span class="str"&gt;"customer"&lt;/span&gt;: &lt;span class="str"&gt;"807023fe77d927e1c222adfe4a0007ba"&lt;/span&gt;
}

{
   &lt;span class="str"&gt;"_id"&lt;/span&gt;: &lt;span class="str"&gt;"807023fe77d927e1c222adfe4a001b53"&lt;/span&gt;,
   &lt;span class="str"&gt;"_rev"&lt;/span&gt;: &lt;span class="str"&gt;"2-9dbf739124353363d53d1ba6671aac4e"&lt;/span&gt;,
   &lt;span class="str"&gt;"type"&lt;/span&gt;: &lt;span class="str"&gt;"order"&lt;/span&gt;,
   &lt;span class="str"&gt;"amount"&lt;/span&gt;: 58,
   &lt;span class="str"&gt;"customer"&lt;/span&gt;: &lt;span class="str"&gt;"807023fe77d927e1c222adfe4a0007ba"&lt;/span&gt;
}

{
   &lt;span class="str"&gt;"_id"&lt;/span&gt;: &lt;span class="str"&gt;"807023fe77d927e1c222adfe4a002516"&lt;/span&gt;,
   &lt;span class="str"&gt;"_rev"&lt;/span&gt;: &lt;span class="str"&gt;"1-5cb6d3729618eb7db0f9285237fd8308"&lt;/span&gt;,
   &lt;span class="str"&gt;"firstName"&lt;/span&gt;: &lt;span class="str"&gt;"Ned"&lt;/span&gt;,
   &lt;span class="str"&gt;"lastName"&lt;/span&gt;: &lt;span class="str"&gt;"Flanders"&lt;/span&gt;,
   &lt;span class="str"&gt;"type"&lt;/span&gt;: &lt;span class="str"&gt;"customer"&lt;/span&gt;
}

{
   &lt;span class="str"&gt;"_id"&lt;/span&gt;: &lt;span class="str"&gt;"807023fe77d927e1c222adfe4a002a4f"&lt;/span&gt;,
   &lt;span class="str"&gt;"_rev"&lt;/span&gt;: &lt;span class="str"&gt;"1-cc5eb0d8c6db0f698c53f9078d16db3b"&lt;/span&gt;,
   &lt;span class="str"&gt;"type"&lt;/span&gt;: &lt;span class="str"&gt;"order"&lt;/span&gt;,
   &lt;span class="str"&gt;"amount"&lt;/span&gt;: 75,
   &lt;span class="str"&gt;"customer"&lt;/span&gt;: &lt;span class="str"&gt;"807023fe77d927e1c222adfe4a002516"&lt;/span&gt;
}&lt;/pre&gt;
&lt;style type="text/css"&gt;.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
&lt;/style&gt;

&lt;p align="justify"&gt;Here we have two customers with their respective orders. The first customer has two orders and the second customer has only one order. Notice that the documents for the orders each have an attribute that contains the ID of the corresponding customer document.&amp;nbsp; &lt;/p&gt;
&lt;p align="justify"&gt;The final goal is that we want to retrieve all customers with their corresponding orders using a single &lt;a href="http://vanryswyckjan.blogspot.com/2009/07/views-into-couchdb.html" target="_blank"&gt;view&lt;/a&gt;. In order to accomplish this we have to make use of complex keys in our map function:&lt;/p&gt;&lt;pre style="width: 100%; height: 147px" class="csharpcode"&gt;&lt;span class="kwrd"&gt;function&lt;/span&gt;(document) {
  &lt;span class="kwrd"&gt;if&lt;/span&gt;(document.type == &lt;span class="str"&gt;'customer'&lt;/span&gt;) {
    emit([document._id, 0], document);
  }
  &lt;span class="kwrd"&gt;else&lt;/span&gt; &lt;span class="kwrd"&gt;if&lt;/span&gt;(document.type == &lt;span class="str"&gt;'order'&lt;/span&gt;) {
    emit([document.customer, 1], document);
  } 
}&lt;/pre&gt;
&lt;style type="text/css"&gt;.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
&lt;/style&gt;

&lt;p align="justify"&gt;The key is composed of the ID of a customer and an&amp;nbsp; arbitrary number used for sorting (collating). For this number we’ve used 0 for customer documents and&amp;nbsp; 1 for order documents. Because the orders will also be sorted by customer ID and their sorting number is higher than the equivalent number for customer documents, the order documents for a particular customer will always come after the customer document itself. This is how the output of the view looks like:&lt;/p&gt;
&lt;p align="justify"&gt;&lt;a href="http://lh4.ggpht.com/-ZvclOQ6ZThE/TzWfW3kryiI/AAAAAAAABoo/-1ccJ9AvSD0/s1600-h/image%25255B6%25255D.png"&gt;&lt;img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://lh6.ggpht.com/-f7f_nHhZZm8/TzWfXlISaZI/AAAAAAAABow/pvwKKXT2d8Q/image_thumb%25255B4%25255D.png?imgmax=800" width="600" height="165"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p align="justify"&gt;Here we see the first customer and his respective orders, followed by the second customer and his single order. This way we can have our cake and eat it too.&lt;/p&gt;
&lt;p align="justify"&gt;We can use extra filters as well. Suppose that we want to retrieve the data and all the associated orders for one specific customer, we can easily use the &lt;em&gt;startkey/endkey&lt;/em&gt; query options in order to get a subset of the view rows:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p align="left"&gt;?startkey=["807023fe77d927e1c222adfe4a0007ba"]&amp;amp;endkey=["807023fe77d927e1c222adfe4a0007ba", 2]&lt;/p&gt;&lt;/blockquote&gt;
&lt;p align="justify"&gt;Using this technique can be quite useful for a couple of scenarios. Being able to make use of the sorting features for complex keys is actually quite nice.&amp;nbsp; &lt;/p&gt;
&lt;p align="justify"&gt;Until next time&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/ShareTheIntellectualWealth/~4/cyRgdidU6Wc" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/ShareTheIntellectualWealth/~3/cyRgdidU6Wc/view-collation-in-couchdb.html</link><author>noreply@blogger.com (Jan Van Ryswyck)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://lh6.ggpht.com/-f7f_nHhZZm8/TzWfXlISaZI/AAAAAAAABow/pvwKKXT2d8Q/s72-c/image_thumb%25255B4%25255D.png?imgmax=800" height="72" width="72" /><thr:total>0</thr:total><feedburner:origLink>http://vanryswyckjan.blogspot.com/2012/02/view-collation-in-couchdb.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-15643421.post-8855460789148181495</guid><pubDate>Tue, 24 Jan 2012 11:00:00 +0000</pubDate><atom:updated>2012-01-24T12:00:03.582+01:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Node.js</category><title>Introducing TrackMyRun</title><description>&lt;p align="justify"&gt;I’ve been working on a small pet project for a couple of weeks now, which I named &lt;a href="https://github.com/JanVanRyswyck/trackmyrun" target="_blank"&gt;TrackMyRun&lt;/a&gt;. I’m quite fanatic when it comes to running, doing about 130 runs a year. Currently I’m keeping track of all these runs in a simple spreadsheet, but off course, that’s certainly not &lt;em&gt;“the geek way”&lt;/em&gt;. Hence the start of yet another pet project.&lt;/p&gt; &lt;p align="justify"&gt;TrackMyRun is written using &lt;a href="http://coffeescript.org/" target="_blank"&gt;CoffeeScript&lt;/a&gt;, &lt;a href="http://nodejs.org/" target="_blank"&gt;Node.js&lt;/a&gt; and &lt;a href="http://expressjs.com/" target="_blank"&gt;Express&lt;/a&gt;. I’m also using the &lt;a href="http://twitter.github.com/bootstrap/" target="_blank"&gt;Bootstrap toolkit&lt;/a&gt; from Twitter for styling. I’m intending to actually use this small application for myself by hosting it on &lt;a href="http://www.heroku.com/" target="_blank"&gt;Heroku&lt;/a&gt; or some other cloud solution as soon as I’m able to finish the most essential features. It’s far from done yet, but you can already have a look at &lt;a href="https://github.com/JanVanRyswyck/trackmyrun" target="_blank"&gt;the source code on GitHub&lt;/a&gt;.&amp;nbsp; As always, suggestions are welcome.&lt;/p&gt; &lt;p align="justify"&gt;Happy reading!&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/ShareTheIntellectualWealth/~4/pDbd9yP1rmg" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/ShareTheIntellectualWealth/~3/pDbd9yP1rmg/introducing-trackmyrun.html</link><author>noreply@blogger.com (Jan Van Ryswyck)</author><thr:total>0</thr:total><feedburner:origLink>http://vanryswyckjan.blogspot.com/2012/01/introducing-trackmyrun.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-15643421.post-100753416497362106</guid><pubDate>Sat, 21 Jan 2012 00:17:00 +0000</pubDate><atom:updated>2012-01-22T11:48:04.738+01:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Node.js</category><title>Taking Toddler Steps with Node.js - Express Routing</title><description>&lt;p align="justify"&gt;In the previous post I provided a short &lt;a href="http://vanryswyckjan.blogspot.com/2011/12/taking-toddler-steps-with-nodejs.html" target="_blank"&gt;introduction to Express&lt;/a&gt;, a web development framework built on top of &lt;a href="https://github.com/senchalabs/connect" target="_blank"&gt;connect&lt;/a&gt; that is heavily inspired by &lt;a href="http://www.sinatrarb.com/" target="_blank"&gt;Sinatra&lt;/a&gt;. For this post we’ll dive into a couple of styles for dealing with routes in Express. &lt;/p&gt; &lt;p align="justify"&gt;Express simply uses HTTP verbs for its routing API. &lt;/p&gt;&lt;pre style="width: 100%; height: 368px" class="csharpcode"&gt;&lt;span class="rem"&gt;// Index&lt;/span&gt;
app.get(&lt;span class="str"&gt;'/runs'&lt;/span&gt;, &lt;span class="kwrd"&gt;function&lt;/span&gt;(request, response) { });            

&lt;span class="rem"&gt;// New run &lt;/span&gt;
app.get(&lt;span class="str"&gt;'/runs/new'&lt;/span&gt;, &lt;span class="kwrd"&gt;function&lt;/span&gt;(request, response) { });

&lt;span class="rem"&gt;// Create a new run&lt;/span&gt;
app.post(&lt;span class="str"&gt;'/runs'&lt;/span&gt;, &lt;span class="kwrd"&gt;function&lt;/span&gt;(request, response) { });

&lt;span class="rem"&gt;// Show run&lt;/span&gt;
app.get(&lt;span class="str"&gt;'/runs/:id'&lt;/span&gt;, &lt;span class="kwrd"&gt;function&lt;/span&gt;(request, response) { });

&lt;span class="rem"&gt;// Edit run&lt;/span&gt;
app.get(&lt;span class="str"&gt;'/runs/:id/edit'&lt;/span&gt;, &lt;span class="kwrd"&gt;function&lt;/span&gt;(request, response) { });

&lt;span class="rem"&gt;// Update run&lt;/span&gt;
app.put(&lt;span class="str"&gt;'/runs/:id'&lt;/span&gt;, &lt;span class="kwrd"&gt;function&lt;/span&gt;(request, response) { });

&lt;span class="rem"&gt;// Delete run&lt;/span&gt;
app.delete(&lt;span class="str"&gt;'/runs/:id'&lt;/span&gt;, &lt;span class="kwrd"&gt;function&lt;/span&gt;(request, response) { });&lt;/pre&gt;
&lt;p align="justify"&gt;
&lt;style type="text/css"&gt;.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
&lt;/style&gt;
Well, that’s pretty much all that you need to get started. The routes that we specified here are treated as plain old regular expressions. Note that in order to make the &lt;em&gt;put&lt;/em&gt; and &lt;em&gt;delete&lt;/em&gt; routes work, we have to add a hidden field to the view.&lt;/p&gt;&lt;pre style="width: 100%; height: 23px" class="csharpcode"&gt;&lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;input&lt;/span&gt; &lt;span class="attr"&gt;name&lt;/span&gt;&lt;span class="kwrd"&gt;="_method"&lt;/span&gt; &lt;span class="attr"&gt;value&lt;/span&gt;&lt;span class="kwrd"&gt;="PUT"&lt;/span&gt; &lt;span class="attr"&gt;type&lt;/span&gt;&lt;span class="kwrd"&gt;="hidden"&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;style type="text/css"&gt;.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
&lt;/style&gt;

&lt;p align="justify"&gt;But the thing that I personally struggled with the most was finding out a decent way to divide up these routes into separate modules without too much of a hassle. Most sample and demo applications out there that use Express usually have all their routes specified in &lt;a href="https://github.com/alexyoung/nodepad/blob/master/app.js" target="_blank"&gt;a single app.js file&lt;/a&gt;. This is something that I don’t like very much as this can become unmaintainable faster that you might think. Roughly 2000 years ago, there was this great emperor (and many after him) who valued the principle of &lt;a href="http://en.wikipedia.org/wiki/Divide_and_rule" target="_blank"&gt;Divide and Conquer&lt;/a&gt;. In order to create maintainable applications, being able to divide up these routes is quite essential. There are several ways to do this.&lt;/p&gt;
&lt;h4&gt;Express Resource&lt;/h4&gt;
&lt;p&gt;This library enables us provide resourceful routing. As usual, &lt;a href="https://github.com/visionmedia/express-resource" target="_blank"&gt;express-resource&lt;/a&gt; can be installed using npm by using the following command:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&amp;nbsp;&lt;em&gt;npm install express-resource&lt;/em&gt;&lt;/p&gt;&lt;/blockquote&gt;
&lt;p align="justify"&gt;Using express-resource, we can create controller modules and use them from our main module. The following snippet shows how a simple controller looks like:&lt;/p&gt;&lt;pre style="width: 100%; height: 503px" class="csharpcode"&gt;exports.index = &lt;span class="kwrd"&gt;function&lt;/span&gt;(request, response){
    response.send(&lt;span class="str"&gt;'Index runs'&lt;/span&gt;);
};

exports.&lt;span class="kwrd"&gt;new&lt;/span&gt; = &lt;span class="kwrd"&gt;function&lt;/span&gt;(request, response){
    response.send(&lt;span class="str"&gt;'New run'&lt;/span&gt;);
};

exports.create = &lt;span class="kwrd"&gt;function&lt;/span&gt;(request, response){
    response.send(&lt;span class="str"&gt;'Create run'&lt;/span&gt;);
};

exports.show = &lt;span class="kwrd"&gt;function&lt;/span&gt;(request, response){
    response.send(&lt;span class="str"&gt;'Show run '&lt;/span&gt; + request.&lt;span class="kwrd"&gt;params&lt;/span&gt;.id);
};

exports.edit = &lt;span class="kwrd"&gt;function&lt;/span&gt;(request response){
    response.send(&lt;span class="str"&gt;'Edit run '&lt;/span&gt; + request.&lt;span class="kwrd"&gt;params&lt;/span&gt;.id);
};

exports.update = &lt;span class="kwrd"&gt;function&lt;/span&gt;(request, response){
    response.send(&lt;span class="str"&gt;'Update run '&lt;/span&gt; + request.&lt;span class="kwrd"&gt;params&lt;/span&gt;.id);
};

exports.destroy = &lt;span class="kwrd"&gt;function&lt;/span&gt;(request, response){
    response.send(&lt;span class="str"&gt;'Delete run '&lt;/span&gt; + request.&lt;span class="kwrd"&gt;params&lt;/span&gt;.id);
};&lt;/pre&gt;
&lt;style type="text/css"&gt;.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
&lt;/style&gt;

&lt;p&gt;Now in the main module (app.js) we just have to add the following code:&lt;/p&gt;&lt;pre style="width: 100%; height: 86px" class="csharpcode"&gt;&lt;span class="kwrd"&gt;var&lt;/span&gt; resource = require(&lt;span class="str"&gt;'express-resource'&lt;/span&gt;)

&lt;span class="rem"&gt;// ...&lt;/span&gt;

application.resource(&lt;span class="str"&gt;'runs'&lt;/span&gt;, require(&lt;span class="str"&gt;'./routes/runs'&lt;/span&gt;));&lt;/pre&gt;
&lt;p align="justify"&gt;
&lt;style type="text/css"&gt;.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
&lt;/style&gt;
That’s it! Now all these routes are hooked up and ready to use. Express-resource has a few other neat features as well. Check out &lt;a href="http://www.nodetuts.com/tutorials/23-express-resources.html#video" target="_blank"&gt;this episode from Node Tuts&lt;/a&gt; to learn more. &lt;/p&gt;
&lt;p align="justify"&gt;Although this seems like a good solution to divide up routes into controller modules, somehow it doesn’t resonate with me. All routes for a particular resource need to exist in the same module which still feels a bit unwieldy to me. I want to have an even more granular approach.&lt;/p&gt;
&lt;h4&gt;&lt;/h4&gt;
&lt;h4&gt;&lt;/h4&gt;
&lt;h4&gt;&lt;/h4&gt;
&lt;h4&gt;&lt;/h4&gt;
&lt;h4&gt;&lt;/h4&gt;
&lt;h4&gt;&lt;/h4&gt;
&lt;h4&gt;Super-duper Require&lt;/h4&gt;
&lt;p&gt;What I like to do is to separate routes based on their context:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;runs/index.js ( index route ) 
&lt;li&gt;runs/new.js ( new and create routes ) 
&lt;li&gt;runs/show.js ( show route ) 
&lt;li&gt;runs/edit.js ( edit and update routes ) 
&lt;li&gt;runs/delete.js ( delete route )&lt;/li&gt;&lt;/ul&gt;
&lt;p align="justify"&gt;Wouldn’t it be cool if we could just &lt;em&gt;“require”&lt;/em&gt; the &lt;em&gt;runs&lt;/em&gt; directory and hook up all routes exported by all the modules that exist in this directory? Well, meet super_duper_require! While still using express-resource, we can now add all these routes like so:&lt;/p&gt;
&lt;ul&gt;&lt;pre style="width: 100%; height: 22px" class="csharpcode"&gt;application.resource(&lt;span class="str"&gt;'runs'&lt;/span&gt;, super_duper_require(module, &lt;span class="str"&gt;'./routes/runs/'&lt;/span&gt;));&lt;/pre&gt;
&lt;style type="text/css"&gt;.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
&lt;/style&gt;
&lt;/ul&gt;
&lt;p align="justify"&gt;This is how the super_duper_require function looks like:&lt;/p&gt;
&lt;ul&gt;&lt;pre style="width: 100%; height: 197px" class="csharpcode"&gt;_ = require(&lt;span class="str"&gt;'underscore'&lt;/span&gt;);

&lt;span class="kwrd"&gt;function&lt;/span&gt; super_duper_require(mod, path) {
    &lt;span class="kwrd"&gt;var&lt;/span&gt; mixin = {};
      fileSystem.readdirSync(path)
          .forEach(&lt;span class="kwrd"&gt;function&lt;/span&gt;(filename) {
            _.extend(mixin, mod.require(path + filename));
        });

      &lt;span class="kwrd"&gt;return&lt;/span&gt; mixin;
};&lt;/pre&gt;
&lt;style type="text/css"&gt;.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
&lt;/style&gt;
&lt;/ul&gt;
&lt;p align="justify"&gt;We just use the magnificent &lt;a href="http://documentcloud.github.com/underscore/" target="_blank"&gt;underscore.js&lt;/a&gt; library here to hook things up. This is just one of the fancy ways to solve the granularity problem. If we don’t want to use the express-resource library, we can always accomplish the same thing by going “plain old school” style.&lt;/p&gt;
&lt;h4 align="justify"&gt;Plain Old School&lt;/h4&gt;
&lt;p align="justify"&gt;This is how I currently set up routing with Express. We no longer need the express-resource library for setting up our routes, but we can still use the same granularity as shown earlier. We also need &lt;em&gt;underscore.js&lt;/em&gt; again, just as in the previous example, in order to stitch things together.&lt;/p&gt;
&lt;ul&gt;&lt;pre style="width: 100%; height: 329px" class="csharpcode"&gt;&lt;span class="kwrd"&gt;var&lt;/span&gt; routes = require(&lt;span class="str"&gt;'./routes'&lt;/span&gt;);
&lt;span class="kwrd"&gt;var&lt;/span&gt; routes.runs = _.extend(require(&lt;span class="str"&gt;'./routes/runs'&lt;/span&gt;), 
                           require(&lt;span class="str"&gt;'./routes/runs/new'&lt;/span&gt;),
                           require(&lt;span class="str"&gt;'./routes/runs/show'&lt;/span&gt;),
                           require(&lt;span class="str"&gt;'./routes/runs/edit'&lt;/span&gt;),
                           require(&lt;span class="str"&gt;'./routes/runs/delete'&lt;/span&gt;));
                           
...

&lt;span class="kwrd"&gt;function&lt;/span&gt; bootstrapRoutes(application) {
    app.get(&lt;span class="str"&gt;'/runs'&lt;/span&gt;, routes.runs.index);            
    app.get(&lt;span class="str"&gt;'/runs/new'&lt;/span&gt;, routes.runs.&lt;span class="kwrd"&gt;new&lt;/span&gt;);
    app.post(&lt;span class="str"&gt;'/runs'&lt;/span&gt;, routes.runs.create);
    app.get(&lt;span class="str"&gt;'/runs/:id'&lt;/span&gt;, routes.runs.show);
    app.get(&lt;span class="str"&gt;'/runs/:id/edit'&lt;/span&gt;, routes.runs.edit);
    app.put(&lt;span class="str"&gt;'/runs/:id'&lt;/span&gt;, routes.runs.update);
    app.delete(&lt;span class="str"&gt;'/runs/:id'&lt;/span&gt;, routes.runs.delete);
}&lt;/pre&gt;
&lt;style type="text/css"&gt;.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
&lt;/style&gt;
&lt;/ul&gt;
&lt;p align="justify"&gt;Up until now I’m pretty happy with this approach. I would love to hear how others divide up their routes into several modules. So please let me know if there are other awesomely cool ways to deal with this. In the mean time, I hope this helps.&lt;/p&gt;
&lt;p align="justify"&gt;Until next time. &lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/ShareTheIntellectualWealth/~4/AOCMhLHFx6E" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/ShareTheIntellectualWealth/~3/AOCMhLHFx6E/taking-toddler-steps-with-nodejs.html</link><author>noreply@blogger.com (Jan Van Ryswyck)</author><thr:total>0</thr:total><feedburner:origLink>http://vanryswyckjan.blogspot.com/2012/01/taking-toddler-steps-with-nodejs.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-15643421.post-1868878734194587635</guid><pubDate>Fri, 30 Dec 2011 20:44:00 +0000</pubDate><atom:updated>2011-12-30T21:44:55.090+01:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Core Skills</category><category domain="http://www.blogger.com/atom/ns#">Community</category><title>Retrospective of 2011, Looking Ahead to 2012</title><description>&lt;p align="justify"&gt;Yep, that time of the year again. Shiny new calendars and the accompanying festivities are upon us again. I can’t get rid of the feeling that every year passes by a lot faster than the year before. This year was definitely not an exception in that regard and I’m afraid that things are not going to improve in 2012.&amp;nbsp;&amp;nbsp; &lt;/p&gt; &lt;p align="justify"&gt;Nonetheless, a lot of stuff happened this year, good and not so good. First of all, I changed jobs twice this year. I’ve been working at &lt;a href="http://vanryswyckjan.blogspot.com/2011/09/my-first-day-at-ichoosr.html" target="_blank"&gt;iChoosr&lt;/a&gt; for a couple of months now and it still feels like I’ve finally come home. I really enjoy working there, my colleagues are awesome folks and I learned a ton already. I do hope to have at least the same amount of fun next year.&lt;/p&gt; &lt;p align="justify"&gt;While enjoying my .NET job during the day, I’ve been doing a lot of Node.js hacking in my spare time, which you could probably tell looking the amount of blog posts I’ve been writing on this topic this year. This has all been a lot of good fun. I have a couple of more blog posts on Node.js lined up for next year, so stay tuned.&lt;/p&gt; &lt;p align="justify"&gt;The new programming language I learned this year was &lt;a href="http://vanryswyckjan.blogspot.com/2011/06/exploring-coffeescript-part-1-and-then.html" target="_blank"&gt;CoffeeScript&lt;/a&gt;, which looks a lot like Ruby. Unfortunately, Ruby is still on my wish list for new programming languages to learn as is &lt;a href="http://clojure.org/" target="_blank"&gt;Clojure&lt;/a&gt; and/or &lt;a href="http://www.scala-lang.org/" target="_blank"&gt;Scala&lt;/a&gt;. I wonder which of these I’m going to pick up in 2012?&lt;/p&gt; &lt;p align="justify"&gt;One of the things that I’m eager to learn about is another operating system. I’ve been soaking in a lot of Unix/Linux stuff during the last couple of months. I’ve been using and developing software for the Windows platform like forever and learning an entire new OS has not been easy. But I must say that it’s well worth the time and effort. I’ve been developing Node.js libraries and applications in my spare time entirely on &lt;a href="http://www.ubuntu.com/" target="_blank"&gt;Ubuntu&lt;/a&gt; and I’m looking forward to take a peek at &lt;a href="http://linuxmint.com/" target="_blank"&gt;Linux Mint&lt;/a&gt; as well. Perhaps I’ll completely switch over to Linux next year, I’m not entirely sure about that yet.&lt;/p&gt; &lt;p align="justify"&gt;I’ve been facilitating a good number of &lt;a href="http://europevan.blogspot.com/" target="_blank"&gt;European VAN&lt;/a&gt; sessions throughout the year. 2011 has been the third year for the E-VAN and I must say that it’s been a fun ride. But I do feel that the time has come for me to move on. I must admit that I only opened Visual Studio a couple of times in my spare time, and then only by accident. Being really honest with myself, I’m just not that interested anymore in the latest and greatest in the .NET space compared to only a couple of years ago. I think I somehow hit a saturation point. So I’m no longer going to organize and/or host new E-VAN sessions. I’ll be more than happy to pass the torch to other developers out there who are willing to step up to the plate. I hereby want to thank all the speakers and those who contributed in the discussions for all their efforts. The recordings are still there and I do hope many folks were able to learn something. I know I most certainly did.&lt;/p&gt; &lt;p align="justify"&gt;Having a job closer to home and also being able to work from home at least once a week ensures that I’m able to spend more time with my family. I’m probably most thankful for that. Working out has been a lot of fun this year as well. I was able to drastically improve myself by running longer distances and also running a lot faster. I’m planning to participate in even more street runs during the next year. I’m looking forward to sustainably improve even further without overloading my body. This is definitely one of the biggest challenges for the upcoming year.&amp;nbsp; &lt;/p&gt; &lt;p align="justify"&gt;All that’s left for me here is to wish you all the best for the new year!&lt;/p&gt; &lt;p align="justify"&gt;Until next year.&amp;nbsp; &lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/ShareTheIntellectualWealth/~4/LtqnCBlKyzM" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/ShareTheIntellectualWealth/~3/LtqnCBlKyzM/retrospective-of-2011-looking-ahead-to.html</link><author>noreply@blogger.com (Jan Van Ryswyck)</author><thr:total>0</thr:total><feedburner:origLink>http://vanryswyckjan.blogspot.com/2011/12/retrospective-of-2011-looking-ahead-to.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-15643421.post-8424735782966451759</guid><pubDate>Fri, 23 Dec 2011 23:12:00 +0000</pubDate><atom:updated>2012-01-20T22:14:21.684+01:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Node.js</category><title>Taking Toddler Steps with Node.js – Express</title><description>&lt;p&gt;&amp;lt; The list of previous installments can be found &lt;a href="http://vanryswyckjan.blogspot.com/search/label/Node.js"&gt;here&lt;/a&gt;. &amp;gt;&lt;/p&gt; &lt;p align="justify"&gt;There are several frameworks out there for building web applications with Node.js, one being more successful than the other. &lt;a href="http://expressjs.com/"&gt;Express&lt;/a&gt; is probably the most popular and well known web development framework for Node.js. It’s heavily inspired by &lt;a href="http://www.sinatrarb.com/"&gt;Sinatra&lt;/a&gt; and built on top of &lt;a href="https://github.com/senchalabs/connect"&gt;connect&lt;/a&gt;, which provides a middleware layer in order to easily add several capabilities and therefore extend your web application. As with most Node.js libraries, Express can be easily installed using &lt;a href="http://npmjs.org/"&gt;npm&lt;/a&gt;.&lt;/p&gt; &lt;blockquote&gt; &lt;p align="justify"&gt;&lt;font style="background-color: #ffffff"&gt;&lt;em&gt;npm install express&lt;/em&gt;&lt;/font&gt;&lt;/p&gt;&lt;/blockquote&gt; &lt;p align="justify"&gt;The following code snippet shows the most basic HTTP server example using Express:&lt;/p&gt;&lt;pre style="width: 100%; height: 137px" class="csharpcode"&gt;&lt;span class="kwrd"&gt;var&lt;/span&gt; express = require(&lt;span class="str"&gt;'express'&lt;/span&gt;);
&lt;span class="kwrd"&gt;var&lt;/span&gt; application = express.createServer();

application.get(&lt;span class="str"&gt;'/'&lt;/span&gt;, &lt;span class="kwrd"&gt;function&lt;/span&gt;(request, response) {
    response.send(&lt;span class="str"&gt;'Hello Express!!'&lt;/span&gt;);
});

application.listen(2455);&lt;/pre&gt;
&lt;style type="text/css"&gt;.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
&lt;/style&gt;

&lt;p align="justify"&gt;Notice that this doesn’t differ that much from most Node.js “Hello World” examples out there. So if we point our browser to &lt;em&gt;localhost:2455&lt;/em&gt;, then the expected text message appears on the page. Now let’s add some middleware into the mix.&lt;/p&gt;
&lt;p align="justify"&gt;Suppose we want to serve some static content like HTML, CSS and/or JavaScript files. We can simply accomplish that by adding the following line of code.&lt;/p&gt;&lt;pre style="width: 100%; height: 19px" class="csharpcode"&gt;application.use(express.&lt;span class="kwrd"&gt;static&lt;/span&gt;(__dirname + &lt;span class="str"&gt;'/public'&lt;/span&gt;));&lt;/pre&gt;
&lt;style type="text/css"&gt;.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
&lt;/style&gt;

&lt;p align="justify"&gt;Now we simply have to add a new folder named &lt;em&gt;“public”&lt;/em&gt; to the root folder of our application. In this folder we add an HTML file named &lt;em&gt;“index.html”&lt;/em&gt; with the following content:&lt;/p&gt;&lt;pre style="width: 100%; height: 132px" class="csharpcode"&gt;&lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;html&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;head&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;title&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;Index&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;title&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;head&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;body&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;h1&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;Hello Express!!&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;h1&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;body&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;html&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;p align="justify"&gt;
&lt;style type="text/css"&gt;.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
&lt;/style&gt;
Now we have to point our browser to &lt;em&gt;localhost:2455/index.html&lt;/em&gt; and our static HTML file is served by Express. That’s how easy we can add capabilities to our web application. Express provides you with several built-in middleware like the one we just used in the last example, but there are also several other open-source middleware in the npm repository to choose from.&lt;/p&gt;
&lt;p align="justify"&gt;Middleware is usually added in a callback function that we provide with the &lt;em&gt;configure&lt;/em&gt; method of the &lt;em&gt;application&lt;/em&gt; object.&lt;/p&gt;&lt;pre style="width: 100%; height: 53px" class="csharpcode"&gt;application.configure(&lt;span class="kwrd"&gt;function&lt;/span&gt;() {
    application.use(express.&lt;span class="kwrd"&gt;static&lt;/span&gt;(__dirname + &lt;span class="str"&gt;'/public'&lt;/span&gt;));    
});&lt;/pre&gt;
&lt;p&gt;
&lt;style type="text/css"&gt;.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
&lt;/style&gt;
Express also provides the ability to configure middleware targeted for specific environments like development, staging, production, etc. … .&lt;/p&gt;&lt;pre class="csharpcode"&gt;application.configure(&lt;span class="kwrd"&gt;function&lt;/span&gt;() {
    &lt;span class="rem"&gt;// Shared configuration&lt;/span&gt;
    application.use(express.bodyParser());
});

application.configure(&lt;span class="str"&gt;'development'&lt;/span&gt;, &lt;span class="kwrd"&gt;function&lt;/span&gt;() {
    console.log(&lt;span class="str"&gt;'Configuring middleware for the development environment.'&lt;/span&gt;);
    application.use(express.&lt;span class="kwrd"&gt;static&lt;/span&gt;(__dirname + &lt;span class="str"&gt;'/public_on_development'&lt;/span&gt;));    
});

application.configure(&lt;span class="str"&gt;'staging'&lt;/span&gt;, &lt;span class="kwrd"&gt;function&lt;/span&gt;() {
    console.log(&lt;span class="str"&gt;'Configuring middleware for the staging environment.'&lt;/span&gt;);
    application.use(express.&lt;span class="kwrd"&gt;static&lt;/span&gt;(__dirname + &lt;span class="str"&gt;'/public_on_staging'&lt;/span&gt;));    
});

application.configure(&lt;span class="str"&gt;'production'&lt;/span&gt;, &lt;span class="kwrd"&gt;function&lt;/span&gt;() {
    console.log(&lt;span class="str"&gt;'Configuring middleware for the production environment.'&lt;/span&gt;);
    application.use(express.&lt;span class="kwrd"&gt;static&lt;/span&gt;(__dirname + &lt;span class="str"&gt;'/public'&lt;/span&gt;));    
});&lt;/pre&gt;
&lt;style type="text/css"&gt;.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
&lt;/style&gt;

&lt;style type="text/css"&gt;.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
&lt;/style&gt;

&lt;p align="justify"&gt;Note that we can still provide a separate configuration function for shared configurations between different environments. When we start our web application in the staging environment, we just have to provide the NODE_ENV environment variable.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;NODE_ENV=staging nodemon app.js&lt;/em&gt;&lt;br&gt;&lt;/p&gt;&lt;/blockquote&gt;
&lt;p align="justify"&gt;Now only the global &lt;em&gt;configure&lt;/em&gt; function and the one for the staging environment are called.&lt;/p&gt;
&lt;p align="justify"&gt;Express also provides an executable for generating boilerplate code and setting up a basic web application. This is probably the quickest way to get up and running with Express.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;node_modules/express/bin$ ./express –h&lt;/em&gt;&lt;/p&gt;&lt;em&gt;&lt;/em&gt;&lt;/blockquote&gt;
&lt;blockquote&gt;&lt;em&gt;
&lt;p&gt;Usage: express [options] [path]&lt;/p&gt;
&lt;p&gt;Options:&lt;br&gt;&amp;nbsp; -s, –sessions&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; add session support&lt;br&gt;&amp;nbsp; -t, --template &amp;lt;engine&amp;gt;&amp;nbsp; add template &amp;lt;engine&amp;gt; support (jade|ejs). default=jade&lt;br&gt;&amp;nbsp; -c, --css &amp;lt;engine&amp;gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; add stylesheet &amp;lt;engine&amp;gt; support (stylus). default=plain css&lt;br&gt;&amp;nbsp; -v, –version&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; output framework version&lt;br&gt;&amp;nbsp; -h, –help&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; output help information&lt;br&gt;&lt;/p&gt;&lt;/em&gt;&lt;/blockquote&gt;
&lt;p align="justify"&gt;So when I run the &lt;em&gt;express&lt;/em&gt; executable with the default options, I get the following output:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;./express /home/my_user/my_sample_app&lt;br&gt;&lt;/em&gt;&lt;/p&gt;&lt;/blockquote&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;create : /home/my_user/my_sample_app&lt;br&gt;create : /home/my_user/my_sample_app/package.json&lt;br&gt;create : /home/my_user/my_sample_app/app.js&lt;br&gt;create : /home/my_user/my_sample_app/public&lt;br&gt;create : /home/my_user/my_sample_app/views&lt;br&gt;create : /home/my_user/my_sample_app/views/layout.jade&lt;br&gt;create : /home/my_user/my_sample_app/views/index.jade&lt;br&gt;create : /home/my_user/my_sample_app/routes&lt;br&gt;create : /home/my_user/my_sample_app/routes/index.js&lt;br&gt;create : /home/my_user/my_sample_app/public/javascripts&lt;br&gt;create : /home/my_user/my_sample_app/public/images&lt;br&gt;create : /home/my_user/my_sample_app/public/stylesheets&lt;br&gt;create : /home/my_user/my_sample_app/public/stylesheets/style.css&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;&lt;em&gt;don’t forget to install dependencies:&lt;br&gt;$ cd /home/my_user/my_sample_app &amp;amp;&amp;amp; npm install&lt;br&gt;&lt;/em&gt;&lt;/p&gt;&lt;/blockquote&gt;
&lt;p align="justify"&gt;A &lt;em&gt;package.json&lt;/em&gt; file is created for dependent modules, along with a public folder for JavaScript files and CSS stylesheets and even separate folders for views and routes. This is how the generated &lt;em&gt;app.js&lt;/em&gt; file looks like:&amp;nbsp;&amp;nbsp; &lt;/p&gt;
&lt;style type="text/css"&gt;.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
&lt;/style&gt;
&lt;pre style="width: 100%; height: 669px" class="csharpcode"&gt;&lt;span class="rem"&gt;/**&lt;/span&gt;
&lt;span class="rem"&gt; * Module dependencies.&lt;/span&gt;
&lt;span class="rem"&gt; */&lt;/span&gt;

&lt;span class="kwrd"&gt;var&lt;/span&gt; express = require(&lt;span class="str"&gt;'express'&lt;/span&gt;)
  , routes = require(&lt;span class="str"&gt;'./routes'&lt;/span&gt;)

&lt;span class="kwrd"&gt;var&lt;/span&gt; app = module.exports = express.createServer();

&lt;span class="rem"&gt;// Configuration&lt;/span&gt;

app.configure(&lt;span class="kwrd"&gt;function&lt;/span&gt;(){
  app.set(&lt;span class="str"&gt;'views'&lt;/span&gt;, __dirname + &lt;span class="str"&gt;'/views'&lt;/span&gt;);
  app.set(&lt;span class="str"&gt;'view engine'&lt;/span&gt;, &lt;span class="str"&gt;'jade'&lt;/span&gt;);
  app.use(express.bodyParser());
  app.use(express.methodOverride());
  app.use(app.router);
  app.use(express.&lt;span class="kwrd"&gt;static&lt;/span&gt;(__dirname + &lt;span class="str"&gt;'/public'&lt;/span&gt;));
});

app.configure(&lt;span class="str"&gt;'development'&lt;/span&gt;, &lt;span class="kwrd"&gt;function&lt;/span&gt;(){
  app.use(express.errorHandler({ dumpExceptions: &lt;span class="kwrd"&gt;true&lt;/span&gt;, showStack: &lt;span class="kwrd"&gt;true&lt;/span&gt; })); 
});

app.configure(&lt;span class="str"&gt;'production'&lt;/span&gt;, &lt;span class="kwrd"&gt;function&lt;/span&gt;(){
  app.use(express.errorHandler()); 
});

&lt;span class="rem"&gt;// Routes&lt;/span&gt;

app.get(&lt;span class="str"&gt;'/'&lt;/span&gt;, routes.index);

app.listen(3000);
console.log(&lt;span class="str"&gt;"Express server listening on port %d in %s mode"&lt;/span&gt;, 
       app.address().port, 
       app.settings.env);&lt;/pre&gt;
&lt;style type="text/css"&gt;.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
&lt;/style&gt;

&lt;p align="justify"&gt;As you can see, it’s very easy to get started with Express and adding more capabilities to your web application as you go along. Make sure to watch this &lt;a href="http://www.screenr.com/mAL"&gt;short screencast&lt;/a&gt; in order to get up and running in no time. &lt;/p&gt;
&lt;p align="justify"&gt;I’m planning to put out a couple more blog posts on Express and some very useful middleware that you can use for your applications. So stay tuned.&lt;/p&gt;
&lt;p align="justify"&gt;Until next time. &lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/ShareTheIntellectualWealth/~4/WbI78mXqhg4" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/ShareTheIntellectualWealth/~3/WbI78mXqhg4/taking-toddler-steps-with-nodejs.html</link><author>noreply@blogger.com (Jan Van Ryswyck)</author><thr:total>0</thr:total><feedburner:origLink>http://vanryswyckjan.blogspot.com/2011/12/taking-toddler-steps-with-nodejs.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-15643421.post-6107071579664178536</guid><pubDate>Fri, 16 Dec 2011 21:10:00 +0000</pubDate><atom:updated>2011-12-23T21:29:40.923+01:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Node.js</category><title>Taking Baby Steps with Node.js – Linking Local Packages with npm</title><description>&lt;p&gt;&amp;lt; The list of previous installments can be found &lt;a href="http://vanryswyckjan.blogspot.com/search/label/Node.js"&gt;here&lt;/a&gt;. &amp;gt;&lt;/p&gt; &lt;p align="justify"&gt;I just wanted to share a very neat feature of &lt;a href="http://npmjs.org" target="_blank"&gt;npm&lt;/a&gt; that makes life quite a lot easier when developing libraries for &lt;a href="http://nodejs.org/" target="_blank"&gt;Node.js&lt;/a&gt;. Suppose that we’re building our very own ORM library for Node.js (we all build an ORM at some point in our career, right?). In order to have our dog food and eat it too, we want to start using our ORM library in an application as soon as possible. As with all other modules, we want to use npm in order to add it as a dependency of our application. But we also don’t want to publicly publish our half-baked ORM package to the npm repository either. This is where creating an npm link saves the day.&lt;/p&gt; &lt;p align="justify"&gt;In the local folder of our library we have to create a link by issuing the following command:&lt;/p&gt; &lt;blockquote&gt; &lt;p&gt;&lt;em&gt;~/kick-ass-orm$&amp;nbsp; npm link &lt;/em&gt;&lt;br&gt;&lt;/p&gt;&lt;/blockquote&gt; &lt;p align="justify"&gt;After that we can add this package to &lt;a href="http://vanryswyckjan.blogspot.com/2011/03/taking-baby-steps-with-nodejs.html" target="_blank"&gt;the node-modules folder&lt;/a&gt; of our application by executing the following command:&lt;/p&gt; &lt;blockquote&gt;&lt;em&gt;~/facebook-killer$ npm link kick-ass-orm&lt;/em&gt;&lt;br&gt;&lt;/blockquote&gt; &lt;p align="justify"&gt;There you have it. Notice that a new folder with the name of the package is added to the &lt;em&gt;node_modules&lt;/em&gt; folder of the application. This is simply a &lt;a href="http://en.wikipedia.org/wiki/Symbolic_link" target="_blank"&gt;symlink&lt;/a&gt; to the package folder of our ORM library. This means that when we make changes to this library, those changes are also exposed to our application as well.&lt;/p&gt; &lt;p align="justify"&gt;As soon as we feel comfortable enough about our ORM library, we can make it publicly available in the npm repository. We can then simply remove the link from our application by issuing the following command:&lt;/p&gt; &lt;blockquote&gt;&lt;em&gt;~/facebook-killer$ npm unlink kick-ass-orm&lt;/em&gt;&lt;br&gt;&lt;/blockquote&gt; &lt;p align="justify"&gt;At this point we add our freshly published package to the &lt;em&gt;package.json&lt;/em&gt; file of our application. We can also remove the link from our local package folder by executing the following command:&lt;/p&gt; &lt;blockquote&gt; &lt;p align="justify"&gt;&lt;em&gt;~/kick-ass-orm$ npm unlink &lt;/em&gt;&lt;/p&gt;&lt;/blockquote&gt; &lt;p align="justify"&gt;This particular feature has been very useful to me over the last couple of weeks. &lt;/p&gt; &lt;p align="justify"&gt;npm – is there anything it can’t do? &lt;/p&gt; &lt;p align="justify"&gt;Until next time.&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/ShareTheIntellectualWealth/~4/jCqz8PEhn-Q" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/ShareTheIntellectualWealth/~3/jCqz8PEhn-Q/taking-baby-steps-with-nodejs-linking.html</link><author>noreply@blogger.com (Jan Van Ryswyck)</author><thr:total>0</thr:total><feedburner:origLink>http://vanryswyckjan.blogspot.com/2011/12/taking-baby-steps-with-nodejs-linking.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-15643421.post-3809703544417418701</guid><pubDate>Tue, 18 Oct 2011 10:00:00 +0000</pubDate><atom:updated>2011-10-18T12:00:03.362+02:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">CoffeeScript</category><category domain="http://www.blogger.com/atom/ns#">Node.js</category><title>Taking Baby Steps with Node.js – BDD Style Unit Tests with Jasmine and CoffeeScript</title><description>&lt;p&gt;&amp;lt; The list of previous installments can be found &lt;a href="http://vanryswyckjan.blogspot.com/search/label/Node.js" target="_blank"&gt;here&lt;/a&gt;. &amp;gt;&lt;/p&gt; &lt;p align="justify"&gt;In a previous blog post, I already wrote about &lt;a href="http://vanryswyckjan.blogspot.com/2011/03/taking-baby-steps-with-nodejs-bdd-style.html" target="_blank"&gt;BDD style unit tests&lt;/a&gt; for testing Node.js modules using Jasmine. I really like the conciseness, simplicity and readability of specification style unit tests using Jasmine. But when we pour &lt;a href="http://jashkenas.github.com/coffee-script/" target="_blank"&gt;CoffeeScript&lt;/a&gt; into the mix, then the syntax for BDD style unit tests really starts to get interesting. &lt;/p&gt; &lt;p align="justify"&gt;Here’s a simple example of a Jasmine test suite written in CoffeeScript:&lt;/p&gt;&lt;pre class="csharpcode"&gt;{ BurglarAlarm, BurglarAlarmState } = require(&lt;span class="str"&gt;'burglaralarm'&lt;/span&gt;)
ControlRoom = require(&lt;span class="str"&gt;'controlroom'&lt;/span&gt;)
should = require(&lt;span class="str"&gt;'should'&lt;/span&gt;)
sinon = require(&lt;span class="str"&gt;'sinon'&lt;/span&gt;)

describe &lt;span class="str"&gt;'An armed burglar alarm'&lt;/span&gt;, -&amp;gt;
    _sut = &lt;span class="kwrd"&gt;null&lt;/span&gt;

    beforeEach -&amp;gt;
        _sut = &lt;span class="kwrd"&gt;new&lt;/span&gt; BurglarAlarm()
        _sut.arm()

    describe &lt;span class="str"&gt;'When a break in occurs'&lt;/span&gt;, -&amp;gt;
        beforeEach -&amp;gt;
            _sut.breakIn()

        it &lt;span class="str"&gt;'should indicate that there is a burglary'&lt;/span&gt;, -&amp;gt;
            _sut.state().should.equal(BurglarAlarmState.alarm)

        it &lt;span class="str"&gt;'should trigger the siren'&lt;/span&gt;, -&amp;gt;
            _sut.siren().isRinging().should.be.ok
    
    describe &lt;span class="str"&gt;'When there is being tampered'&lt;/span&gt;, -&amp;gt;
        _mockedControlRoom = &lt;span class="kwrd"&gt;null&lt;/span&gt;

        beforeEach -&amp;gt;
            _mockedControlRoom = sinon.mock(ControlRoom.getInstance())
            _mockedControlRoom.expects(&lt;span class="str"&gt;'notifyTamperAlarm'&lt;/span&gt;).once()
            
            _sut.tamper()
            
        afterEach -&amp;gt;
            _mockedControlRoom.restore()

        it &lt;span class="str"&gt;'should indicate that there is a tamper alarm'&lt;/span&gt;, -&amp;gt;
            _sut.state().should.equal(BurglarAlarmState.tamper)

        it &lt;span class="str"&gt;'should notify the control room'&lt;/span&gt;, -&amp;gt;
            _mockedControlRoom.verify()&lt;/pre&gt;
&lt;style type="text/css"&gt;.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
&lt;/style&gt;

&lt;p align="justify"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p align="justify"&gt;Personally, I really like this syntax. Notice the &lt;a href="http://vanryswyckjan.blogspot.com/2011/08/exploring-coffeescript-part-6-show-me.html" target="_blank"&gt;destructuring assignment&lt;/a&gt; on the first line. This is a small trick we can use when a particular module exports multiple types. We also used a mocking library called &lt;a href="https://github.com/cjohansen/Sinon.JS" target="_blank"&gt;Sinon.js&lt;/a&gt; which is a very powerful library for test spies, stubs and mocks for JavaScript.&lt;/p&gt;
&lt;p align="justify"&gt;Here’s the code for the &lt;em&gt;BurglarAlarm&lt;/em&gt;, &lt;em&gt;ControlRoom&lt;/em&gt; and &lt;em&gt;Siren&lt;/em&gt; modules. Note that this example code is also written using CoffeeScript, but this can be plain old JavaScript as well if you want. &lt;/p&gt;&lt;pre class="csharpcode"&gt;##############################################
# BurglarAlarm module
##############################################
BurglarAlarmState = 
    normal: 0
    armed: 1
    alarm: 2
    tamper: 3

exports.BurglarAlarmState = BurglarAlarmState

&lt;span class="kwrd"&gt;class&lt;/span&gt; exports.BurglarAlarm
    _siren = &lt;span class="kwrd"&gt;null&lt;/span&gt;
    _state = BurglarAlarmState.normal
    
    constructor: -&amp;gt;
        _siren = &lt;span class="kwrd"&gt;new&lt;/span&gt; Siren()    
        
    siren: -&amp;gt; _siren
    state: -&amp;gt; _state

    arm: -&amp;gt; _state = BurglarAlarmState.armed

    breakIn: -&amp;gt; 
        _state = BurglarAlarmState.alarm
        _siren.makeSomeNoise()    

    tamper: -&amp;gt;
        _state = BurglarAlarmState.tamper
        ControlRoom.getInstance().notifyTamperAlarm()

##############################################
# ControlRoom module
##############################################
&lt;span class="kwrd"&gt;class&lt;/span&gt; ControlRoom
    notifyTamperAlarm: -&amp;gt;
        console.log(&lt;span class="str"&gt;'Tamper alarm noticed in the control room ...'&lt;/span&gt;)

instance = &lt;span class="kwrd"&gt;null&lt;/span&gt;

module.exports = {
    getInstance: -&amp;gt;
        unless instance?
            instance = &lt;span class="kwrd"&gt;new&lt;/span&gt; ControlRoom()
        instance
}

##############################################
# Siren module
##############################################
module.exports = &lt;span class="kwrd"&gt;class&lt;/span&gt; Siren
    ringing = &lt;span class="kwrd"&gt;false&lt;/span&gt;

    isRinging: -&amp;gt; ringing
    makeSomeNoise: -&amp;gt; ringing = true&lt;/pre&gt;
&lt;style type="text/css"&gt;.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
&lt;/style&gt;

&lt;p align="justify"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p align="justify"&gt;There you go. Until next time.&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/ShareTheIntellectualWealth/~4/ShYynA4aYBg" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/ShareTheIntellectualWealth/~3/ShYynA4aYBg/taking-baby-steps-with-nodejs-bdd-style.html</link><author>noreply@blogger.com (Jan Van Ryswyck)</author><thr:total>0</thr:total><feedburner:origLink>http://vanryswyckjan.blogspot.com/2011/10/taking-baby-steps-with-nodejs-bdd-style.html</feedburner:origLink></item></channel></rss>
