<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Elegant Code &#187; Jan Van Ryswyck</title>
	<atom:link href="http://elegantcode.com/author/jryswyck/feed/" rel="self" type="application/rss+xml" />
	<link>http://elegantcode.com</link>
	<description></description>
	<lastBuildDate>Fri, 10 Feb 2012 04:38:00 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.5</generator>
		<item>
		<title>Introducing TrackMyRun</title>
		<link>http://elegantcode.com/2012/01/24/introducing-trackmyrun/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=introducing-trackmyrun</link>
		<comments>http://elegantcode.com/2012/01/24/introducing-trackmyrun/#comments</comments>
		<pubDate>Tue, 24 Jan 2012 11:00:00 +0000</pubDate>
		<dc:creator>Jan Van Ryswyck</dc:creator>
				<category><![CDATA[Node.js]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2012/01/24/introducing-trackmyrun/</guid>
		<description><![CDATA[I’ve been working on a small pet project for a couple of weeks now, which I named TrackMyRun. 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 “the geek way”. Hence the [...]]]></description>
			<content:encoded><![CDATA[<p align="justify">I’ve been working on a small pet project for a couple of weeks now, which I named <a href="https://github.com/JanVanRyswyck/trackmyrun" target="_blank">TrackMyRun</a>. 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 <em>“the geek way”</em>. Hence the start of yet another pet project.</p>
<p align="justify">TrackMyRun is written using <a href="http://coffeescript.org/" target="_blank">CoffeeScript</a>, <a href="http://nodejs.org/" target="_blank">Node.js</a> and <a href="http://expressjs.com/" target="_blank">Express</a>. I’m also using the <a href="http://twitter.github.com/bootstrap/" target="_blank">Bootstrap toolkit</a> from Twitter for styling. I’m intending to actually use this small application for myself by hosting it on <a href="http://www.heroku.com/" target="_blank">Heroku</a> 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 <a href="https://github.com/JanVanRyswyck/trackmyrun" target="_blank">the source code on GitHub</a>.&#160; As always, suggestions are welcome.</p>
<p align="justify">Happy reading!</p>
]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2012/01/24/introducing-trackmyrun/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Taking Toddler Steps with Node.js &#8211; Express Routing</title>
		<link>http://elegantcode.com/2012/01/20/taking-toddler-steps-with-node-js-express-routing/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=taking-toddler-steps-with-node-js-express-routing</link>
		<comments>http://elegantcode.com/2012/01/20/taking-toddler-steps-with-node-js-express-routing/#comments</comments>
		<pubDate>Sat, 21 Jan 2012 00:16:04 +0000</pubDate>
		<dc:creator>Jan Van Ryswyck</dc:creator>
				<category><![CDATA[Node.js]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2012/01/20/taking-toddler-steps-with-node-js-express-routing/</guid>
		<description><![CDATA[In the previous post I provided a short introduction to Express, a web development framework built on top of connect that is heavily inspired by Sinatra. For this post we’ll dive into a couple of styles for dealing with routes in Express. Express simply uses HTTP verbs for its routing API. // Index app.get('/runs', function(request, [...]]]></description>
			<content:encoded><![CDATA[<p align="justify">In the previous post I provided a short <a href="http://elegantcode.com/2011/12/23/taking-toddler-steps-with-node-js-express/" target="_blank">introduction to Express</a>, a web development framework built on top of <a href="https://github.com/senchalabs/connect" target="_blank">connect</a> that is heavily inspired by <a href="http://www.sinatrarb.com/" target="_blank">Sinatra</a>. For this post we’ll dive into a couple of styles for dealing with routes in Express. </p>
<p align="justify">Express simply uses HTTP verbs for its routing API. </p>
<pre style="width: 100%; height: 329px" class="csharpcode"><span class="rem">// Index</span>
app.get(<span class="str">'/runs'</span>, <span class="kwrd">function</span>(request, response) { });            

<span class="rem">// New run </span>
app.get(<span class="str">'/runs/new'</span>, <span class="kwrd">function</span>(request, response) { });

<span class="rem">// Create a new run</span>
app.post(<span class="str">'/runs'</span>, <span class="kwrd">function</span>(request, response) { });

<span class="rem">// Show run</span>
app.get(<span class="str">'/runs/:id'</span>, <span class="kwrd">function</span>(request, response) { });

<span class="rem">// Edit run</span>
app.get(<span class="str">'/runs/:id/edit'</span>, <span class="kwrd">function</span>(request, response) { });

<span class="rem">// Update run</span>
app.put(<span class="str">'/runs/:id'</span>, <span class="kwrd">function</span>(request, response) { });

<span class="rem">// Delete run</span>
app.delete(<span class="str">'/runs/:id'</span>, <span class="kwrd">function</span>(request, response) { });</pre>
<p align="justify">
<style type="text/css">
.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; }</style>
<p>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 <em>put</em> and <em>delete</em> routes work, we have to add a hidden field to the view.</p>
<pre style="width: 100%; height: 23px" class="csharpcode"><span class="kwrd">&lt;</span><span class="html">input</span> <span class="attr">name</span><span class="kwrd">=&quot;_method&quot;</span> <span class="attr">value</span><span class="kwrd">=&quot;PUT&quot;</span> <span class="attr">type</span><span class="kwrd">=&quot;hidden&quot;</span><span class="kwrd">&gt;</span></pre>
<style type="text/css">
.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; }</style>
<p align="justify">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 <a href="https://github.com/alexyoung/nodepad/blob/master/app.js" target="_blank">a single app.js file</a>. 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 <a href="http://en.wikipedia.org/wiki/Divide_and_rule" target="_blank">Divide and Conquer</a>. In order to create maintainable applications, being able to divide up these routes is quite essential. There are several ways to do this.</p>
<h4>Express Resource</h4>
<p>This library enables us provide resourceful routing. As usual, <a href="https://github.com/visionmedia/express-resource" target="_blank">express-resource</a> can be installed using npm by using the following command:</p>
<blockquote>
<p>&#160;<em>npm install express-resource</em></p>
</blockquote>
<p align="justify">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:</p>
<pre style="width: 100%; height: 438px" class="csharpcode">exports.index = <span class="kwrd">function</span>(request, response){
    response.send(<span class="str">'Index runs'</span>);
};

exports.<span class="kwrd">new</span> = <span class="kwrd">function</span>(request, response){
    response.send(<span class="str">'New run'</span>);
};

exports.create = <span class="kwrd">function</span>(request, response){
    response.send(<span class="str">'Create run'</span>);
};

exports.show = <span class="kwrd">function</span>(request, response){
    response.send(<span class="str">'Show run '</span> + request.<span class="kwrd">params</span>.id);
};

exports.edit = <span class="kwrd">function</span>(request response){
    response.send(<span class="str">'Edit run '</span> + request.<span class="kwrd">params</span>.id);
};

exports.update = <span class="kwrd">function</span>(request, response){
    response.send(<span class="str">'Update run '</span> + request.<span class="kwrd">params</span>.id);
};

exports.destroy = <span class="kwrd">function</span>(request, response){
    response.send(<span class="str">'Delete run '</span> + request.<span class="kwrd">params</span>.id);
};</pre>
<style type="text/css">
.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; }</style>
<p>Now in the main module (app.js) we just have to add the following code:</p>
<pre style="width: 100%; height: 86px" class="csharpcode"><span class="kwrd">var</span> resource = require(<span class="str">'express-resource'</span>)

<span class="rem">// ...</span>

application.resource(<span class="str">'runs'</span>, require(<span class="str">'./routes/runs'</span>));</pre>
<p align="justify">
<style type="text/css">
.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; }</style>
<p>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 <a href="http://www.nodetuts.com/tutorials/23-express-resources.html#video" target="_blank">this episode from Node Tuts</a> to learn more. </p>
<p align="justify">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.</p>
<h4></h4>
<h4></h4>
<h4></h4>
<h4></h4>
<h4></h4>
<h4></h4>
<h4>Super-duper Require</h4>
<p>What I like to do is to separate routes based on their context:</p>
<ul>
<li>runs/index.js ( index route )</li>
<li>runs/new.js ( new and create routes )</li>
<li>runs/show.js ( show route )</li>
<li>runs/edit.js ( edit and update routes )</li>
<li>runs/delete.js ( delete route )</li>
</ul>
<p align="justify">Wouldn’t it be cool if we could just <em>“require”</em> the <em>runs</em> 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:</p>
<ul>
<pre style="width: 100%; height: 22px" class="csharpcode">application.resource(<span class="str">'runs'</span>, super_duper_require(module, <span class="str">'./routes/runs/'</span>));</pre>
<style type="text/css">
.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; }</style>
<p align="justify">This is how the super_duper_require function looks like:</p>
<pre style="width: 100%; height: 174px" class="csharpcode">_ = require(<span class="str">'underscore'</span>);

<span class="kwrd">function</span> super_duper_require(mod, path) {
    <span class="kwrd">var</span> mixin = {};
      fileSystem.readdirSync(path)
          .forEach(<span class="kwrd">function</span>(filename) {
            _.extend(mixin, mod.require(path + filename));
        });

      <span class="kwrd">return</span> mixin;
};</pre>
<style type="text/css">
.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; }</style>
</ul>
<p align="justify">We just use the magnificent <a href="http://documentcloud.github.com/underscore/" target="_blank">underscore.js</a> 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.</p>
<h4 align="justify">Plain Old School</h4>
<p align="justify">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 <em>underscore.js</em> again, just as in the previous example, in order to stitch things together.</p>
<ul>
<pre style="width: 100%; height: 286px" class="csharpcode"><span class="kwrd">var</span> routes = require(<span class="str">'./routes'</span>);
<span class="kwrd">var</span> routes.runs = _.extend(require(<span class="str">'./routes/runs'</span>),
                           require(<span class="str">'./routes/runs/new'</span>),
                           require(<span class="str">'./routes/runs/show'</span>),
                           require(<span class="str">'./routes/runs/edit'</span>),
                           require(<span class="str">'./routes/runs/delete'</span>));

...

<span class="kwrd">function</span> bootstrapRoutes(application) {
    app.get(<span class="str">'/runs'</span>, routes.runs.index);
    app.get(<span class="str">'/runs/new'</span>, routes.runs.<span class="kwrd">new</span>);
    app.post(<span class="str">'/runs'</span>, routes.runs.create);
    app.get(<span class="str">'/runs/:id'</span>, routes.runs.show);
    app.get(<span class="str">'/runs/:id/edit'</span>, routes.runs.edit);
    app.put(<span class="str">'/runs/:id'</span>, routes.runs.update);
    app.delete(<span class="str">'/runs/:id'</span>, routes.runs.delete);
}</pre>
<style type="text/css">
.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; }</style>
</ul>
<p align="justify">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.</p>
<p align="justify">Until next time. </p>
]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2012/01/20/taking-toddler-steps-with-node-js-express-routing/feed/</wfw:commentRss>
		<slash:comments>13</slash:comments>
		</item>
		<item>
		<title>Retrospective of 2011, Looking Ahead to 2012</title>
		<link>http://elegantcode.com/2011/12/30/retrospective-of-2011-looking-ahead-to-2012/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=retrospective-of-2011-looking-ahead-to-2012</link>
		<comments>http://elegantcode.com/2011/12/30/retrospective-of-2011-looking-ahead-to-2012/#comments</comments>
		<pubDate>Fri, 30 Dec 2011 20:42:49 +0000</pubDate>
		<dc:creator>Jan Van Ryswyck</dc:creator>
				<category><![CDATA[Esoterica]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2011/12/30/retrospective-of-2011-looking-ahead-to-2012/</guid>
		<description><![CDATA[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 [...]]]></description>
			<content:encoded><![CDATA[<p align="justify">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.&#160;&#160; </p>
<p align="justify">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 <a href="http://vanryswyckjan.blogspot.com/2011/09/my-first-day-at-ichoosr.html" target="_blank">iChoosr</a> 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.</p>
<p align="justify">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.</p>
<p align="justify">The new programming language I learned this year was <a href="http://elegantcode.com/2011/06/21/exploring-coffeescript-part-1-and-then-there-was-coffee/" target="_blank">CoffeeScript</a>, which looks a lot like Ruby. Unfortunately, Ruby is still on my wish list for new programming languages to learn as is <a href="http://clojure.org/" target="_blank">Clojure</a> and/or <a href="http://www.scala-lang.org/" target="_blank">Scala</a>. I wonder which of these I’m going to pick up in 2012?</p>
<p align="justify">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 <a href="http://www.ubuntu.com/" target="_blank">Ubuntu</a> and I’m looking forward to take a peek at <a href="http://linuxmint.com/" target="_blank">Linux Mint</a> as well. Perhaps I’ll completely switch over to Linux next year, I’m not entirely sure about that yet.</p>
<p align="justify">I’ve been facilitating a good number of <a href="http://europevan.blogspot.com/" target="_blank">European VAN</a> 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.</p>
<p align="justify">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.&#160; </p>
<p align="justify">All that’s left for me here is to wish you all the best for the new year!</p>
<p align="justify">Until next year.&#160; </p>
]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2011/12/30/retrospective-of-2011-looking-ahead-to-2012/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Taking Toddler Steps with Node.js &#8211; Express</title>
		<link>http://elegantcode.com/2011/12/23/taking-toddler-steps-with-node-js-express/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=taking-toddler-steps-with-node-js-express</link>
		<comments>http://elegantcode.com/2011/12/23/taking-toddler-steps-with-node-js-express/#comments</comments>
		<pubDate>Fri, 23 Dec 2011 23:10:59 +0000</pubDate>
		<dc:creator>Jan Van Ryswyck</dc:creator>
				<category><![CDATA[Node.js]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2011/12/23/taking-toddler-steps-with-node-js-express/</guid>
		<description><![CDATA[&#60; The list of previous installments can be found here. &#62; There are several frameworks out there for building web applications with Node.js, one being more successful than the other. Express is probably the most popular and well known web development framework for Node.js. It’s heavily inspired by Sinatra and built on top of connect, [...]]]></description>
			<content:encoded><![CDATA[<p>&lt; The list of previous installments can be found <a href="http://elegantcode.com/category/node-js/">here</a>. &gt;</p>
<p align="justify">There are several frameworks out there for building web applications with Node.js, one being more successful than the other. <a href="http://expressjs.com/">Express</a> is probably the most popular and well known web development framework for Node.js. It’s heavily inspired by <a href="http://www.sinatrarb.com/">Sinatra</a> and built on top of <a href="https://github.com/senchalabs/connect">connect</a>, 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 <a href="http://npmjs.org/">npm</a>.</p>
<blockquote><p align="justify"><font style="background-color: #ffffff"><em>npm install express</em></font></p>
</blockquote>
<p align="justify">The following code snippet shows the most basic HTTP server example using Express:</p>
<pre style="width: 100%; height: 137px" class="csharpcode"><span class="kwrd">var</span> express = require(<span class="str">'express'</span>);
<span class="kwrd">var</span> application = express.createServer();

application.get(<span class="str">'/'</span>, <span class="kwrd">function</span>(request, response) {
    response.send(<span class="str">'Hello Express!!'</span>);
});

application.listen(2455);</pre>
<style type="text/css">
.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; }</style>
<p align="justify">Notice that this doesn’t differ that much from most Node.js “Hello World” examples out there. So if we point our browser to <em>localhost:2455</em>, then the expected text message appears on the page. Now let’s add some middleware into the mix.</p>
<p align="justify">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.</p>
<pre style="width: 100%; height: 19px" class="csharpcode">application.use(express.<span class="kwrd">static</span>(__dirname + <span class="str">'/public'</span>));</pre>
<style type="text/css">
.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; }</style>
<p align="justify">Now we simply have to add a new folder named <em>“public”</em> to the root folder of our application. In this folder we add an HTML file named <em>“index.html”</em> with the following content:</p>
<pre style="width: 100%; height: 132px" class="csharpcode"><span class="kwrd">&lt;</span><span class="html">html</span><span class="kwrd">&gt;</span>
    <span class="kwrd">&lt;</span><span class="html">head</span><span class="kwrd">&gt;</span>
        <span class="kwrd">&lt;</span><span class="html">title</span><span class="kwrd">&gt;</span>Index<span class="kwrd">&lt;/</span><span class="html">title</span><span class="kwrd">&gt;</span>
    <span class="kwrd">&lt;/</span><span class="html">head</span><span class="kwrd">&gt;</span>
    <span class="kwrd">&lt;</span><span class="html">body</span><span class="kwrd">&gt;</span>
        <span class="kwrd">&lt;</span><span class="html">h1</span><span class="kwrd">&gt;</span>Hello Express!!<span class="kwrd">&lt;/</span><span class="html">h1</span><span class="kwrd">&gt;</span>
    <span class="kwrd">&lt;/</span><span class="html">body</span><span class="kwrd">&gt;</span>
<span class="kwrd">&lt;/</span><span class="html">html</span><span class="kwrd">&gt;</span></pre>
<p align="justify">
<style type="text/css">
.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; }</style>
<p>Now we have to point our browser to <em>localhost:2455/index.html</em> 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.</p>
<p align="justify">Middleware is usually added in a callback function that we provide with the <em>configure</em> method of the <em>application</em> object.</p>
<pre style="width: 100%; height: 53px" class="csharpcode">application.configure(<span class="kwrd">function</span>() {
    application.use(express.<span class="kwrd">static</span>(__dirname + <span class="str">'/public'</span>));
});</pre>
<p>
<style type="text/css">
.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; }</style>
<p>Express also provides the ability to configure middleware targeted for specific environments like development, staging, production, etc. … .</p>
<pre class="csharpcode">application.configure(<span class="kwrd">function</span>() {
    <span class="rem">// Shared configuration</span>
    application.use(express.bodyParser());
});

application.configure(<span class="str">'development'</span>, <span class="kwrd">function</span>() {
    console.log(<span class="str">'Configuring middleware for the development environment.'</span>);
    application.use(express.<span class="kwrd">static</span>(__dirname + <span class="str">'/public_on_development'</span>));
});

application.configure(<span class="str">'staging'</span>, <span class="kwrd">function</span>() {
    console.log(<span class="str">'Configuring middleware for the staging environment.'</span>);
    application.use(express.<span class="kwrd">static</span>(__dirname + <span class="str">'/public_on_staging'</span>));
});

application.configure(<span class="str">'production'</span>, <span class="kwrd">function</span>() {
    console.log(<span class="str">'Configuring middleware for the production environment.'</span>);
    application.use(express.<span class="kwrd">static</span>(__dirname + <span class="str">'/public'</span>));
});</pre>
<style type="text/css">
.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; }</style>
<style type="text/css">
.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; }</style>
<p align="justify">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.</p>
<blockquote>
<p><em>NODE_ENV=staging nodemon app.js</em></p>
<p></p>
</blockquote>
<p align="justify">Now only the global <em>configure</em> function and the one for the staging environment are called.</p>
<p align="justify">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.</p>
<blockquote>
<p><em>node_modules/express/bin$ ./express –h</em></p>
<p>  <em></em></p></blockquote>
<blockquote><p><em></p>
<p>Usage: express [options] [path]</p>
<p>Options:<br />
      <br />&#160; -s, –sessions&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; add session support</p>
<p>&#160; -t, &#8211;template &lt;engine&gt;&#160; add template &lt;engine&gt; support (jade|ejs). default=jade</p>
<p>&#160; -c, &#8211;css &lt;engine&gt;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; add stylesheet &lt;engine&gt; support (stylus). default=plain css</p>
<p>&#160; -v, –version&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; output framework version</p>
<p>&#160; -h, –help&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; output help information</p>
<p></p>
<p>  </em></p></blockquote>
<p align="justify">So when I run the <em>express</em> executable with the default options, I get the following output:</p>
<blockquote>
<p><em>./express /home/my_user/my_sample_app<br />
      <br /></em></p>
</blockquote>
<blockquote>
<p><em>create : /home/my_user/my_sample_app<br />
      <br />create : /home/my_user/my_sample_app/package.json</p>
<p>create : /home/my_user/my_sample_app/app.js</p>
<p>create : /home/my_user/my_sample_app/public</p>
<p>create : /home/my_user/my_sample_app/views</p>
<p>create : /home/my_user/my_sample_app/views/layout.jade</p>
<p>create : /home/my_user/my_sample_app/views/index.jade</p>
<p>create : /home/my_user/my_sample_app/routes</p>
<p>create : /home/my_user/my_sample_app/routes/index.js</p>
<p>create : /home/my_user/my_sample_app/public/javascripts</p>
<p>create : /home/my_user/my_sample_app/public/images</p>
<p>create : /home/my_user/my_sample_app/public/stylesheets</p>
<p>create : /home/my_user/my_sample_app/public/stylesheets/style.css</em></p>
<p><em>don’t forget to install dependencies:<br />
      <br /> $ cd /home/my_user/my_sample_app &amp;&amp; npm install</p>
<p></em></p>
</blockquote>
<p align="justify">A <em>package.json</em> 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 <em>app.js</em> file looks like:&#160;&#160; </p>
<style type="text/css">
.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; }</style>
<pre style="width: 100%; height: 587px" class="csharpcode"><span class="rem">/**</span>
<span class="rem"> * Module dependencies.</span>
<span class="rem"> */</span>

<span class="kwrd">var</span> express = require(<span class="str">'express'</span>)
  , routes = require(<span class="str">'./routes'</span>)

<span class="kwrd">var</span> app = module.exports = express.createServer();

<span class="rem">// Configuration</span>

app.configure(<span class="kwrd">function</span>(){
  app.set(<span class="str">'views'</span>, __dirname + <span class="str">'/views'</span>);
  app.set(<span class="str">'view engine'</span>, <span class="str">'jade'</span>);
  app.use(express.bodyParser());
  app.use(express.methodOverride());
  app.use(app.router);
  app.use(express.<span class="kwrd">static</span>(__dirname + <span class="str">'/public'</span>));
});

app.configure(<span class="str">'development'</span>, <span class="kwrd">function</span>(){
  app.use(express.errorHandler({ dumpExceptions: <span class="kwrd">true</span>, showStack: <span class="kwrd">true</span> }));
});

app.configure(<span class="str">'production'</span>, <span class="kwrd">function</span>(){
  app.use(express.errorHandler());
});

<span class="rem">// Routes</span>

app.get(<span class="str">'/'</span>, routes.index);

app.listen(3000);
console.log(<span class="str">&quot;Express server listening on port %d in %s mode&quot;</span>,
       app.address().port,
       app.settings.env);</pre>
<style type="text/css">
.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; }</style>
<p align="justify">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 <a href="http://www.screenr.com/mAL">short screencast</a> in order to get up and running in no time. </p>
<p align="justify">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.</p>
<p align="justify">Until next time. </p>
]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2011/12/23/taking-toddler-steps-with-node-js-express/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Taking Baby Steps with Node.js &#8211; Linking Local Packages with npm</title>
		<link>http://elegantcode.com/2011/12/16/taking-baby-steps-with-node-js-linking-local-packages-with-npm/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=taking-baby-steps-with-node-js-linking-local-packages-with-npm</link>
		<comments>http://elegantcode.com/2011/12/16/taking-baby-steps-with-node-js-linking-local-packages-with-npm/#comments</comments>
		<pubDate>Fri, 16 Dec 2011 21:07:59 +0000</pubDate>
		<dc:creator>Jan Van Ryswyck</dc:creator>
				<category><![CDATA[Node.js]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2011/12/16/taking-baby-steps-with-node-js-linking-local-packages-with-npm/</guid>
		<description><![CDATA[&#60; The list of previous installments can be found here. &#62; I just wanted to share a very neat feature of npm that makes life quite a lot easier when developing libraries for Node.js. Suppose that we’re building our very own ORM library for Node.js (we all build an ORM at some point in our [...]]]></description>
			<content:encoded><![CDATA[<p> &lt; The list of previous installments can be found <a href="http://elegantcode.com/category/node-js/">here</a>. &gt;</p>
<p align="justify">I just wanted to share a very neat feature of <a href="http://npmjs.org" target="_blank">npm</a> that makes life quite a lot easier when developing libraries for <a href="http://nodejs.org/" target="_blank">Node.js</a>. 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.</p>
<p align="justify">In the local folder of our library we have to create a link by issuing the following command:</p>
<blockquote><p><em>~/kick-ass-orm$&#160; npm link </em>      </p>
</blockquote>
<p align="justify">After that we can add this package to <a href="http://elegantcode.com/2011/03/17/taking-baby-steps-with-node-js-node_modules-folders/" target="_blank">the node-modules folder</a> of our application by executing the following command:</p>
<blockquote><p><em>~/facebook-killer$ npm link kick-ass-orm</em>    </p></blockquote>
<p align="justify">There you have it. Notice that a new folder with the name of the package is added to the <em>node_modules</em> folder of the application. This is simply a <a href="http://en.wikipedia.org/wiki/Symbolic_link" target="_blank">symlink</a> 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.</p>
<p align="justify">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:</p>
<blockquote><p><em>~/facebook-killer$ npm unlink kick-ass-orm</em>    </p></blockquote>
<p align="justify">At this point we add our freshly published package to the <em>package.json</em> file of our application. We can also remove the link from our local package folder by executing the following command:</p>
<blockquote><p align="justify"><em>~/kick-ass-orm$ npm unlink </em></p>
</blockquote>
<p align="justify">This particular feature has been very useful to me over the last couple of weeks. </p>
<p align="justify">npm – is there anything it can’t do? </p>
<p align="justify">Until next time.</p>
]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2011/12/16/taking-baby-steps-with-node-js-linking-local-packages-with-npm/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Taking Baby Steps with Node.js &#8211; BDD Style Unit Tests with Jasmine and CoffeeScript</title>
		<link>http://elegantcode.com/2011/10/18/taking-baby-steps-with-node-js-bdd-style-unit-tests-with-jasmine-and-coffeescript/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=taking-baby-steps-with-node-js-bdd-style-unit-tests-with-jasmine-and-coffeescript</link>
		<comments>http://elegantcode.com/2011/10/18/taking-baby-steps-with-node-js-bdd-style-unit-tests-with-jasmine-and-coffeescript/#comments</comments>
		<pubDate>Tue, 18 Oct 2011 10:00:00 +0000</pubDate>
		<dc:creator>Jan Van Ryswyck</dc:creator>
				<category><![CDATA[CoffeeScript]]></category>
		<category><![CDATA[Node.js]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2011/10/18/taking-baby-steps-with-node-js-bdd-style-unit-tests-with-jasmine-and-coffeescript/</guid>
		<description><![CDATA[&#60; The list of previous installments can be found here. &#62; In a previous blog post, I already wrote about BDD style unit tests 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 CoffeeScript into the mix, then the [...]]]></description>
			<content:encoded><![CDATA[<p>&lt; The list of previous installments can be found <a href="http://elegantcode.com/category/node-js/" target="_blank">here</a>. &gt;</p>
<p align="justify">In a previous blog post, I already wrote about <a href="http://elegantcode.com/2011/03/07/taking-baby-steps-with-node-js-bdd-style-unit-tests-with-jasmine-node-sprinkled-with-some-should/" target="_blank">BDD style unit tests</a> 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 <a href="http://jashkenas.github.com/coffee-script/" target="_blank">CoffeeScript</a> into the mix, then the syntax for BDD style unit tests really starts to get interesting. </p>
<p align="justify">Here’s a simple example of a Jasmine test suite written in CoffeeScript:</p>
<pre class="csharpcode">{ BurglarAlarm, BurglarAlarmState } = require(<span class="str">'burglaralarm'</span>)
ControlRoom = require(<span class="str">'controlroom'</span>)
should = require(<span class="str">'should'</span>)
sinon = require(<span class="str">'sinon'</span>)

describe <span class="str">'An armed burglar alarm'</span>, -&gt;
    _sut = <span class="kwrd">null</span>

    beforeEach -&gt;
        _sut = <span class="kwrd">new</span> BurglarAlarm()
        _sut.arm()

    describe <span class="str">'When a break in occurs'</span>, -&gt;
        beforeEach -&gt;
            _sut.breakIn()

        it <span class="str">'should indicate that there is a burglary'</span>, -&gt;
            _sut.state().should.equal(BurglarAlarmState.alarm)

        it <span class="str">'should trigger the siren'</span>, -&gt;
            _sut.siren().isRinging().should.be.ok

    describe <span class="str">'When there is being tampered'</span>, -&gt;
        _mockedControlRoom = <span class="kwrd">null</span>

        beforeEach -&gt;
            _mockedControlRoom = sinon.mock(ControlRoom.getInstance())
            _mockedControlRoom.expects(<span class="str">'notifyTamperAlarm'</span>).once()

            _sut.tamper()

        afterEach -&gt;
            _mockedControlRoom.restore()

        it <span class="str">'should indicate that there is a tamper alarm'</span>, -&gt;
            _sut.state().should.equal(BurglarAlarmState.tamper)

        it <span class="str">'should notify the control room'</span>, -&gt;
            _mockedControlRoom.verify()</pre>
<style type="text/css">
.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; }</style>
<p align="justify">&#160;</p>
<p align="justify">Personally, I really like this syntax. Notice the <a href="http://elegantcode.com/2011/08/09/exploring-coffeescript-part-6-show-me-the-goodies/" target="_blank">destructuring assignment</a> 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 <a href="https://github.com/cjohansen/Sinon.JS" target="_blank">Sinon.js</a> which is a very powerful library for test spies, stubs and mocks for JavaScript.</p>
<p align="justify">Here’s the code for the <em>BurglarAlarm</em>, <em>ControlRoom</em> and <em>Siren</em> modules. Note that this example code is also written using CoffeeScript, but this can be plain old JavaScript as well if you want. </p>
<pre class="csharpcode">##############################################
# BurglarAlarm module
##############################################
BurglarAlarmState =
    normal: 0
    armed: 1
    alarm: 2
    tamper: 3

exports.BurglarAlarmState = BurglarAlarmState

<span class="kwrd">class</span> exports.BurglarAlarm
    _siren = <span class="kwrd">null</span>
    _state = BurglarAlarmState.normal

    constructor: -&gt;
        _siren = <span class="kwrd">new</span> Siren()    

    siren: -&gt; _siren
    state: -&gt; _state

    arm: -&gt; _state = BurglarAlarmState.armed

    breakIn: -&gt;
        _state = BurglarAlarmState.alarm
        _siren.makeSomeNoise()    

    tamper: -&gt;
        _state = BurglarAlarmState.tamper
        ControlRoom.getInstance().notifyTamperAlarm()

##############################################
# ControlRoom module
##############################################
<span class="kwrd">class</span> ControlRoom
    notifyTamperAlarm: -&gt;
        console.log(<span class="str">'Tamper alarm noticed in the control room ...'</span>)

instance = <span class="kwrd">null</span>

module.exports = {
    getInstance: -&gt;
        unless instance?
            instance = <span class="kwrd">new</span> ControlRoom()
        instance
}

##############################################
# Siren module
##############################################
module.exports = <span class="kwrd">class</span> Siren
    ringing = <span class="kwrd">false</span>

    isRinging: -&gt; ringing
    makeSomeNoise: -&gt; ringing = true</pre>
<style type="text/css">
.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; }</style>
<p align="justify">&#160;</p>
<p align="justify">There you go. Until next time.</p>
]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2011/10/18/taking-baby-steps-with-node-js-bdd-style-unit-tests-with-jasmine-and-coffeescript/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>The Cathedral and the Bazaar</title>
		<link>http://elegantcode.com/2011/10/14/the-cathedral-and-the-bazaar/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=the-cathedral-and-the-bazaar</link>
		<comments>http://elegantcode.com/2011/10/14/the-cathedral-and-the-bazaar/#comments</comments>
		<pubDate>Fri, 14 Oct 2011 22:26:21 +0000</pubDate>
		<dc:creator>Jan Van Ryswyck</dc:creator>
				<category><![CDATA[Esoterica]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2011/10/14/the-cathedral-and-the-bazaar/</guid>
		<description><![CDATA[A couple of weeks ago, I ran into this website from Eric S. Raymond, the author of the book The Cathedral and the Bazaar. There’s a recording of a great presentation that anyone involved with software development, and especially open-source, should listen to. I highly recommended that you download this recording and at the very [...]]]></description>
			<content:encoded><![CDATA[<p align="justify">A couple of weeks ago, I ran into <a href="http://catb.org/~esr/writings/homesteading/" target="_blank">this website from Eric S. Raymond</a>, the author of the book <a href="http://www.amazon.com/exec/obidos/ASIN/0596001088/elegantcode-20" target="_blank">The Cathedral and the Bazaar</a>. There’s a <a href="http://catb.org/~esr/writings/homesteading/linux1_d50_96kbs.mp3" target="_blank">recording of a great presentation</a> that anyone involved with software development, and especially open-source, should listen to. I highly recommended that you download this recording and at the very least listen to it twice! You can also <a href="http://catb.org/~esr/writings/homesteading/cathedral-bazaar/" target="_blank">read the book online</a>. </p>
<p align="justify">Enjoy!</p>
]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2011/10/14/the-cathedral-and-the-bazaar/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
<enclosure url="http://catb.org/~esr/writings/homesteading/linux1_d50_96kbs.mp3" length="23993887" type="audio/mpeg" />
		</item>
		<item>
		<title>Presenting &#8220;Taking Baby Steps with Node.js&#8221; at Agile.NET 2011 Europe</title>
		<link>http://elegantcode.com/2011/09/23/presenting-taking-baby-steps-with-node-js-at-agile-net-2011-europe/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=presenting-taking-baby-steps-with-node-js-at-agile-net-2011-europe</link>
		<comments>http://elegantcode.com/2011/09/23/presenting-taking-baby-steps-with-node-js-at-agile-net-2011-europe/#comments</comments>
		<pubDate>Fri, 23 Sep 2011 20:20:53 +0000</pubDate>
		<dc:creator>Jan Van Ryswyck</dc:creator>
				<category><![CDATA[Conferences]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2011/09/23/presenting-taking-baby-steps-with-node-js-at-agile-net-2011-europe/</guid>
		<description><![CDATA[In a couple of weeks, I’ll be doing an introductory presentation on Node.js at the Agile.NET 2011 conference. While having a look at the other sessions and speakers, I’m quite honored to be able to join these smart bunch of craftsmen. In fact, I’m still amazed that the organizers accepted my proposal. But hey, they’ll [...]]]></description>
			<content:encoded><![CDATA[<p align="justify">In a couple of weeks, I’ll be doing an introductory presentation on <a href="http://nodejs.org/">Node.js</a> at the <a href="http://www.agileminds.be/event/5">Agile.NET 2011 conference</a>. While having a look at the other sessions and speakers, I’m quite honored to be able to join these smart bunch of craftsmen. In fact, I’m still amazed that the organizers accepted my proposal. But hey, they’ll probably let me do my talk somewhere in the basement anyway <img src='http://elegantcode.com/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> . </p>
<p align="justify">You probably figured out by now that you can’t afford to miss these two days of high quality learning, especially if you live in the European part of the world. If you live in Belgium or the Netherlands, you simply don’t have any valid excuse for not attending. </p>
<p align="justify">If you haven’t registered yet, be sure to do this in the upcoming week in order to get an early-bird discount. And in addition, I can even hook you up with an additional 50 Euro discount. Just drop me a line.</p>
<p align="justify">Hope to see you there.&#160; </p>
]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2011/09/23/presenting-taking-baby-steps-with-node-js-at-agile-net-2011-europe/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>My First Day at iChoosr</title>
		<link>http://elegantcode.com/2011/09/12/my-first-day-at-ichoosr/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=my-first-day-at-ichoosr</link>
		<comments>http://elegantcode.com/2011/09/12/my-first-day-at-ichoosr/#comments</comments>
		<pubDate>Mon, 12 Sep 2011 19:22:47 +0000</pubDate>
		<dc:creator>Jan Van Ryswyck</dc:creator>
				<category><![CDATA[Esoterica]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2011/09/12/my-first-day-at-ichoosr/</guid>
		<description><![CDATA[Today was my very first day at iChoosr, a small internet startup that focuses on Vendor Relationship Management. I’ll be working there as a web developer in a small team of very bright people. Working for a small startup is a totally new experience for me. So I’m very excited about what the future will [...]]]></description>
			<content:encoded><![CDATA[<p align="justify">Today was my very first day at <a href="http://www.ichoosr.com/nl/index.php?s=about" target="_blank">iChoosr</a>, a small internet startup that focuses on <a href="http://en.wikipedia.org/wiki/Vendor_Relationship_Management" target="_blank">Vendor Relationship Management</a>. I’ll be working there as a web developer in a small team of very bright people. Working for a small startup is a totally new experience for me. So I’m very excited about what the future will bring.&#160;&#160; </p>
<p align="justify">Pretty sure there’s lots of learning ahead of me <img style="border-bottom-style: none; border-right-style: none; border-top-style: none; border-left-style: none" class="wlEmoticon wlEmoticon-smile" alt="Smile" src="http://elegantcode.com/wp-content/uploads/2011/09/wlEmoticon-smile.png" /></p>
]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2011/09/12/my-first-day-at-ichoosr/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>An Observation about TDD</title>
		<link>http://elegantcode.com/2011/09/09/an-observation-about-tdd/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=an-observation-about-tdd</link>
		<comments>http://elegantcode.com/2011/09/09/an-observation-about-tdd/#comments</comments>
		<pubDate>Fri, 09 Sep 2011 18:31:54 +0000</pubDate>
		<dc:creator>Jan Van Ryswyck</dc:creator>
				<category><![CDATA[TDD]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2011/09/09/an-observation-about-tdd/</guid>
		<description><![CDATA[To me, developers that are not applying TDD practices during their day-to-day job always seem more in a hurry than developers that do apply red-green-refactor. In their hurry, they&#8217;re the first to cut corners and start making messes while they rush to their goal. A while ago it dawned to me why that is. They [...]]]></description>
			<content:encoded><![CDATA[<p align="justify">To me, developers that are not applying TDD practices during their day-to-day job always seem more in a hurry than developers that do apply red-green-refactor. In their hurry, they&#8217;re the first to cut corners and start making messes while they rush to their goal. A while ago it dawned to me why that is. They subconsciously want to get feedback as soon as possible about the code they&#8217;re writing. They cut corners and generally mess up their code in order to prevent spending those extra hours and days to keep things clean. Constantly refactoring and cleaning up their code is restraining them from having the feedback they so desperately want.</p>
<p align="justify">Humans are in fact feedback junkies. We constantly want to know how we&#8217;re doing what we&#8217;re doing. I actually wrote <a href="http://vanryswyckjan.blogspot.com/2007/05/agile-is-just-human-nature.html" target="_blank">a blog post</a> about this a couple of years ago.</p>
<p align="justify">Since I adopted TDD as a discipline, I tend to feel less pressured which results in me taking the time to continuously refactor the code I&#8217;m working on, trying to keep everything clean. Why? Because I known that the code I wrote a minute ago works. The tests I write constantly give me a shot of feedback so that I&#8217;m constantly hooked. </p>
<p align="justify">Just an observation &#8230;</p>
]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2011/09/09/an-observation-about-tdd/feed/</wfw:commentRss>
		<slash:comments>21</slash:comments>
		</item>
	</channel>
</rss>

