<?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: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/" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" version="2.0">

<channel>
	<title>Rob Searles</title>
	
	<link>http://www.robsearles.com</link>
	<description>Musing on the business of and development for "The Web"</description>
	<lastBuildDate>Thu, 02 Feb 2012 15:36:21 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/RobSearles" /><feedburner:info uri="robsearles" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><item>
		<title>I’ve Turned into a Hipster</title>
		<link>http://feedproxy.google.com/~r/RobSearles/~3/-DoNBQjRfq0/</link>
		<comments>http://www.robsearles.com/2012/02/02/ive-turned-into-a-hipster/#comments</comments>
		<pubDate>Thu, 02 Feb 2012 15:36:21 +0000</pubDate>
		<dc:creator>Rob Searles</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[coffeescript]]></category>
		<category><![CDATA[hipster]]></category>
		<category><![CDATA[nodejs]]></category>

		<guid isPermaLink="false">http://www.robsearles.com/?p=602</guid>
		<description><![CDATA[After playing around with CoffeeScript, I find it is too good not to use. So now I am a CoffeeScript coder. Does this mean I'm a hipster? How embarrassing.]]></description>
			<content:encoded><![CDATA[<p style="text-align: center; line-height: 14.25pt; background: white;" align="center"><img class="aligncenter size-full wp-image-624" style="font-family: Georgia; font-size: 10pt; background-color: white; line-height: 14.25pt;" title="Hipster Dog" src="http://www.robsearles.com/wp-content/uploads/2012/01/Hipster-Dog-Is-Here.jpg" alt="Hipster Dog" width="600" height="300" /></p>
<p style="line-height: 14.25pt; background: white;"><span style="font-size: 10.0pt; font-family: Georgia;" lang="EN-CA">I’ve seen a day I never thought would come:  the day I turned into a hipster!</span></p>
<p style="line-height: 14.25pt; background: white;"><span style="font-size: 10.0pt; font-family: Georgia;" lang="EN-CA">If you&#8217;ve been following this blog, you&#8217;ll have noticed that throughout 2011 I&#8217;ve been working<span> </span></span><span lang="EN-CA"><a title="Node.JS: Experiments with Processes" href="http://www.robsearles.com/2011/09/28/nodejs-experiments-with-processes/"><span style="font-size: 10.0pt; font-family: Georgia;">more</span></a></span><span><span style="font-size: 10.0pt; font-family: Georgia;" lang="EN-CA"> </span></span><span style="font-size: 10.0pt; font-family: Georgia;" lang="EN-CA">and<span> </span></span><span lang="EN-CA"><a title="Node.JS: Experiments with the Middle End, Part 1" href="http://www.robsearles.com/2011/10/20/nodejs-experiments-with-middle-end-part1/"><span style="font-size: 10.0pt; font-family: Georgia;">more</span></a></span><span><span style="font-size: 10.0pt; font-family: Georgia;" lang="EN-CA"> </span></span><span style="font-size: 10.0pt; font-family: Georgia;" lang="EN-CA">in Javascript, more specifically with<span> </span></span><span lang="EN-CA"><a href="http://nodejs.org/" target="_blank"><span style="font-size: 10.0pt; font-family: Georgia;">Node.JS</span></a></span><span style="font-size: 10.0pt; font-family: Georgia;" lang="EN-CA">.</span></p>
<p style="line-height: 14.25pt; background: white;"><span style="font-size: 10.0pt; font-family: Georgia;" lang="EN-CA">The last few months have been especially Node.JS-centric. During the day, I&#8217;ve been building architectural improvements into<span> </span><a href="http://www.jabbakam.com/" target="_blank">Jabbakam</a>. Evenings and weekends, I’ve been  hacking about on the<span> </span></span><span lang="EN-CA"><a href="http://www.liveunsigned.com/" target="_blank"><span style="font-size: 10.0pt; font-family: Georgia;">Live Unsigned</span></a></span><span><span style="font-size: 10.0pt; font-family: Georgia;" lang="EN-CA"> </span></span><span style="font-size: 10.0pt; font-family: Georgia;" lang="EN-CA">mobile site. All of my recent projects revolve around Node.JS, and I&#8217;ve been loving it. Node is incredibly interesting and powerful, and it is giving my brain a workout.</span></p>
<p style="line-height: 14.25pt; background: white;"><span style="font-size: 10.0pt; font-family: Georgia;" lang="EN-CA">However, Javascript itself has a few niggly bits that become more problematic the larger the project gets. Issues with structure and maintainability of code motivate projects such as<span> </span><a href="http://joose.it/" target="_blank">Joose</a><span> </span>and </span><span lang="EN-CA"><a href="http://documentcloud.github.com/backbone/" target="_blank"><span style="font-size: 10.0pt; font-family: Georgia;">Backbone.js</span></a></span><span style="font-size: 10.0pt; font-family: Georgia;" lang="EN-CA">. One of the things that has been bugging me is having to hack in class-based inheritance. After a lot of experimentation, I settled on<span> </span></span><span lang="EN-CA"><a href="http://javascript.crockford.com/prototypal.html" target="_blank"><span style="font-size: 10.0pt; font-family: Georgia;">Prototypal Inheritance</span></a></span><span style="font-size: 10.0pt; font-family: Georgia;" lang="EN-CA">, which does the job to an extent, but left me dissatisfied .<a name="_GoBack"></a></span></p>
<p style="line-height: 14.25pt; background: white;"><span style="font-size: 10.0pt; font-family: Georgia;" lang="EN-CA">So, the other day I finally broke down and tried<span> </span><a href="http://coffeescript.org/" target="_blank">CoffeeScript</a>.</span></p>
<p style="line-height: 14.25pt; background: white;"><span style="font-size: 10.0pt; font-family: Georgia;" lang="EN-CA">When CoffeeScript first came out I resolved never to bother with it. It is clearly for<span> </span></span><span lang="EN-CA"><a href="http://www.youtube.com/watch?v=cAy4zULKFDU" target="_blank"><span style="font-size: 10.0pt; font-family: Georgia;">Ruby people</span></a></span><span><span style="font-size: 10.0pt; font-family: Georgia;" lang="EN-CA"> </span></span><span style="font-size: 10.0pt; font-family: Georgia;" lang="EN-CA">who don&#8217;t quite understand Javascript. Since then it&#8217;s been getting a<span> </span></span><span lang="EN-CA"><a href="http://net.tutsplus.com/tutorials/javascript-ajax/rocking-out-with-coffeescript/" target="_blank"><span style="font-size: 10.0pt; font-family: Georgia;">lot</span></a></span><span><span style="font-size: 10.0pt; font-family: Georgia;" lang="EN-CA"> </span></span><span lang="EN-CA"><a href="http://amix.dk/blog/post/19612" target="_blank"><span style="font-size: 10.0pt; font-family: Georgia;">of</span></a></span><span><span style="font-size: 10.0pt; font-family: Georgia;" lang="EN-CA"> </span></span><span lang="EN-CA"><a href="http://dailyjs.com/2011/05/30/code-review-coffeescript/" target="_blank"><span style="font-size: 10.0pt; font-family: Georgia;">press</span></a></span><span style="font-size: 10.0pt; font-family: Georgia;" lang="EN-CA">, and it now even has<span> </span><a href="http://pragprog.com/book/tbcoffee/coffeescript" target="_blank">its own book</a>!</span></p>
<p style="line-height: 14.25pt; background: white;"><span style="font-size: 10.0pt; font-family: Georgia;" lang="EN-CA">Anyway, I was up the other night, waiting for my daughter to wake up in screaming pain as razor-sharp pieces of enamel slowly bored through her gums. With nothing better to do, I decided to play around with CoffeeScript.</span></p>
<p style="line-height: 14.25pt; background: white;"><span style="font-size: 10.0pt; font-family: Georgia;" lang="EN-CA">I was up and running in no time, installing the<span> </span></span><span lang="EN-CA"><a href="http://search.npmjs.org/#/coffee-script" target="_blank"><span style="font-size: 10.0pt; font-family: Georgia;">Node.JS Module</span></a></span><span><span style="font-size: 10.0pt; font-family: Georgia;" lang="EN-CA"> </span></span><span style="font-size: 10.0pt; font-family: Georgia;" lang="EN-CA">and browsing the<span> </span></span><span lang="EN-CA"><a href="http://arcturo.github.com/library/coffeescript/" target="_blank"><span style="font-size: 10.0pt; font-family: Georgia;">Little Book of CoffeeScript</span></a></span><span style="font-size: 10.0pt; font-family: Georgia;" lang="EN-CA">. After about an hour I had effortlessly written some additions to Live Unsigned which integrated seamlessly. To be honest, I feel like a bit of a muppet for not trying it sooner.</span></p>
<p style="line-height: 14.25pt; background: white;"><span style="font-size: 10.0pt; font-family: Georgia;" lang="EN-CA">After just a couple of days playing around with it, CoffeeScript has now become a major part of my toolkit. The syntax is elegant, nicely lending itself to clean coding, and code can be reused easily, improving project structure. CoffeeScript compiles to pretty clean Javascript, and from what I can make out, the better you understand Javascript, the better CoffeeScript you can write.</span></p>
<p style="line-height: 14.25pt; background: white;"><span style="font-size: 10.0pt; font-family: Georgia;" lang="EN-CA">So yes, I am now coding in CoffeeScript.</span></p>
<p style="line-height: 14.25pt; background: white;"><span style="font-size: 10.0pt; font-family: Georgia;" lang="EN-CA">I am now a hipster.</span></p>
<p style="line-height: 14.25pt; background: white;"><span style="font-size: 10.0pt; font-family: Georgia;" lang="EN-CA">There is nothing left for me but to rush out, buy some skinny jeans and a Macbook Air and learn Vim.</span></p>
<p style="line-height: 14.25pt; background: white;"><span style="font-size: 10.0pt; font-family: Georgia;" lang="EN-CA">Oh, God.</span></p>
<img src="http://www.robsearles.com/?ak_action=api_record_view&id=602&type=feed" alt="" /><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/RobSearles?a=-DoNBQjRfq0:lLOMiy8JrOE:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/RobSearles?d=yIl2AUoC8zA" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/RobSearles/~4/-DoNBQjRfq0" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.robsearles.com/2012/02/02/ive-turned-into-a-hipster/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://www.robsearles.com/2012/02/02/ive-turned-into-a-hipster/</feedburner:origLink></item>
		<item>
		<title>2012 US Politics</title>
		<link>http://feedproxy.google.com/~r/RobSearles/~3/M9V7g-nM54s/</link>
		<comments>http://www.robsearles.com/2012/01/07/2012-us-politics/#comments</comments>
		<pubDate>Sat, 07 Jan 2012 22:15:32 +0000</pubDate>
		<dc:creator>Rob Searles</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[politics]]></category>

		<guid isPermaLink="false">http://www.robsearles.com/?p=597</guid>
		<description><![CDATA[This is a a Saturday night jot, so feel free to skip right on past this.
Now, I&#8217;m not American or any of that nonsense, but it seems the 2012 US elections are already heating up, especially with all this talk of SOPA, PIPA etc.
After all the hope (or hype?) that was Obama there are some [...]]]></description>
			<content:encoded><![CDATA[<p><em>This is a a Saturday night jot, so feel free to skip right on past this.</em></p>
<p>Now, I&#8217;m not American or any of that nonsense, but it seems the 2012 US elections are already heating up, especially with all this talk of <a href="http://en.wikipedia.org/wiki/Stop_Online_Piracy_Act" target="_blank">SOPA</a>, <a href="http://act.demandprogress.org/letter/pipa_sopa" target="_blank">PIPA</a> etc.</p>
<p>After all the hope (or hype?) that was Obama there are some new contenders for the presidency.  Out of all of these, <a href="http://www.ronpaul2012.com/" target="_blank">Ron Paul</a> seems to be the only one making any sort of sense (caveat, I may very well be wrong about this, but he actually talks about the bad things the US has done!). <a href="http://mittromneyisatool.com/" target="_blank">Mitt Romney</a> sees to have that strange chiselled jaw only Americans aspire. And <a href="http://spreadingsantorum.com/" target="_blank">Senator Rick Santorum</a>? Well, let&#8217;s just say in my own tiny way, I&#8217;m doing my bit.</p>
<img src="http://www.robsearles.com/?ak_action=api_record_view&id=597&type=feed" alt="" /><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/RobSearles?a=M9V7g-nM54s:qTpPUpkfmMI:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/RobSearles?d=yIl2AUoC8zA" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/RobSearles/~4/M9V7g-nM54s" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.robsearles.com/2012/01/07/2012-us-politics/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://www.robsearles.com/2012/01/07/2012-us-politics/</feedburner:origLink></item>
		<item>
		<title>NodeJS: Experiments with Middle End Part 3</title>
		<link>http://feedproxy.google.com/~r/RobSearles/~3/q07RLDbFcIE/</link>
		<comments>http://www.robsearles.com/2011/12/07/nodejs-experiments-with-middle-end-part-3/#comments</comments>
		<pubDate>Wed, 07 Dec 2011 12:53:47 +0000</pubDate>
		<dc:creator>Rob Searles</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[experiments]]></category>
		<category><![CDATA[nodejs]]></category>

		<guid isPermaLink="false">http://www.robsearles.com/?p=529</guid>
		<description><![CDATA[Note:  This is the last in a series of posts describing my experiments with constructing a Middle End. In Part 1, we discussed the concept of the middle end and its advantages. In Part 2, we constructed a very simple example.
In this post, we&#8217;re going to make that example slightly more complex by introducing [...]]]></description>
			<content:encoded><![CDATA[<div style="background: #FFFEEB; margin: 10px 0 0 0; padding: 5px 10px; border: 1px solid #ccc;"><strong>Note: </strong> This is the last in a series of posts describing my experiments with constructing a Middle End. In <a href="http://www.robsearles.com/2011/10/20/nodejs-experiments-with-middle-end-part1/">Part 1</a>, we discussed the concept of the middle end and its advantages. In <a href="http://www.robsearles.com/2011/12/02/nodejs-experiments-with-middle-end-part-2">Part 2</a>, we constructed a very simple example.</div>
<p>In this post, we&#8217;re going to make that example slightly more complex by introducing dependencies within the modules. Now the code must be able to handle these dependencies for both the client side and the server side.</p>
<p>In the<a href="http://www.robsearles.com/2011/12/02/nodejs-experiments-with-middle-end-part-2"> last post</a>, we built a simple module consisting of a single function that returned a greeting:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> Greetings <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #009900;">&#125;</span>
Greetings.<span style="color: #660066;">prototype</span>.<span style="color: #660066;">hello</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>who<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
   <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #3366CC;">&quot;Hello &quot;</span><span style="color: #339933;">+</span>who;
<span style="color: #009900;">&#125;</span>
module.<span style="color: #660066;">exports</span> <span style="color: #339933;">=</span> Greetings;</pre></div></div>

<p>At this point, it would be very useful to sanitize and validate the parameter passed to the &#8220;hello&#8221; function. We could obviously build this functionality directly into the code, but this is exactly the sort of thing that should be abstracted into its own module(s).</p>
<p>For the purpose of this example, we&#8217;ll simply include the functionality to give the parameter passed a &#8220;trim&#8221;; after some hacking and snippet-borrowing from <a href="https://github.com/chriso/node-validator" target="_blank">node-validator</a> by Chris O&#8217;Hara, we can build another simple module called Tidy:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> Tidy <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
   whitespace<span style="color: #339933;">:</span> <span style="color: #3366CC;">'<span style="color: #000099; font-weight: bold;">\\</span>r<span style="color: #000099; font-weight: bold;">\\</span>n<span style="color: #000099; font-weight: bold;">\\</span>t<span style="color: #000099; font-weight: bold;">\\</span>s'</span><span style="color: #339933;">,</span>
&nbsp;
   trim<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>str<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #003366; font-weight: bold;">var</span> whitespace <span style="color: #339933;">=</span>  <span style="color: #3366CC;">'<span style="color: #000099; font-weight: bold;">\\</span>r<span style="color: #000099; font-weight: bold;">\\</span>n<span style="color: #000099; font-weight: bold;">\\</span>t<span style="color: #000099; font-weight: bold;">\\</span>s'</span>;
      str <span style="color: #339933;">=</span> str.<span style="color: #660066;">replace</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">new</span> RegExp<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'^['</span><span style="color: #339933;">+</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">whitespace</span><span style="color: #339933;">+</span><span style="color: #3366CC;">']+|['</span><span style="color: #339933;">+</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">whitespace</span><span style="color: #339933;">+</span><span style="color: #3366CC;">']+$'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'g'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">''</span><span style="color: #009900;">&#41;</span>
      <span style="color: #000066; font-weight: bold;">return</span> str;
   <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
module.<span style="color: #660066;">exports</span> <span style="color: #339933;">=</span> Tidy;</pre></div></div>

<p>We can now improve our Greetings module, including the dependency and functionality:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> Tidy <span style="color: #339933;">=</span> require<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'./tidy'</span><span style="color: #009900;">&#41;</span>;
&nbsp;
<span style="color: #003366; font-weight: bold;">var</span> Greetings <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #009900;">&#125;</span>
Greetings.<span style="color: #660066;">prototype</span>.<span style="color: #660066;">hello</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>who<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
   who <span style="color: #339933;">=</span> Tidy.<span style="color: #660066;">trim</span><span style="color: #009900;">&#40;</span>who<span style="color: #009900;">&#41;</span>;
   <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">hello</span><span style="color: #009900;">&#40;</span>who<span style="color: #009900;">&#41;</span>;
<span style="color: #009900;">&#125;</span>
module.<span style="color: #660066;">exports</span> <span style="color: #339933;">=</span> Greetings;</pre></div></div>

<p>To see if this will work as expected, let&#8217;s add some spaces to the parameter passed in the NodeJS script:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> Greetings <span style="color: #339933;">=</span> require<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'./greetings'</span><span style="color: #009900;">&#41;</span>;
<span style="color: #003366; font-weight: bold;">var</span> greetings <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Greetings<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;
console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span>greetings.<span style="color: #660066;">hello</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'  Node   '</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">+</span><span style="color: #3366CC;">&quot;!&quot;</span><span style="color: #009900;">&#41;</span>;</pre></div></div>

<p>When we run it, this is what we see:</p>

<div class="wp_syntax"><div class="code"><pre class="bash bash" style="font-family:monospace;">$ node runner.js
Hello Node<span style="color: #000000; font-weight: bold;">!</span></pre></div></div>

<p>Good, so this works on the server side, but let’s test its behaviour on the client side. Again, we should add some spaces to the parameter passed to ensure that it is working:</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;script type=&quot;text/javascript&quot;&gt; &lt;/script&gt;
&lt;script src=&quot;greetings.js&quot; type=&quot;text/javascript&quot;&gt; &lt;/script&gt;
&lt;script type=&quot;text/javascript&quot;&gt; &lt;/script&gt;</pre></div></div>

<p>Unfortunately, we now see the following error:</p>

<div class="wp_syntax"><div class="code"><pre class="text text" style="font-family:monospace;">require is not defined
[Break On This Error] var greetings = new Greetings();</pre></div></div>

<p>Our attempt to &#8220;require&#8221; the dependency in the Greetings module has broken the client side. To fix this, we need to define our own <strong>require()</strong> function, much like we had to for the <strong>module</strong> object. We do this by creating a new front end-only script, which we&#8217;ll call &#8220;middle-end.js&#8221;.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> require <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>module<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
   document.<span style="color: #000066; font-weight: bold;">write</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'&lt;script src=&quot;'</span><span style="color: #339933;">+</span>module<span style="color: #339933;">+</span><span style="color: #3366CC;">'.js&quot; type=&quot;text/javascript&quot;&gt; &lt;/script&gt;'</span><span style="color: #009900;">&#41;</span>;
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">var</span> module <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
   exports<span style="color: #339933;">:</span> undefined
<span style="color: #009900;">&#125;</span>;</pre></div></div>

<p><em>(Obviously we should include much more error checking, but this will be enough for our example).</em></p>
<p>We modify our html as follows:</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;script src=&quot;middle-end.js&quot; type=&quot;text/javascript&quot;&gt; &lt;/script&gt;
&lt;script type=&quot;text/javascript&quot;&gt; &lt;/script&gt;
....</pre></div></div>

<p>Now when we reload the page in the browser, all should work as expected. Huzzah!</p>
<p>This is just a very simple example of what can be done when JavaScript code is shared between the client and server sides—the &#8220;Middle End&#8221;. This concept becomes really exciting when you start thinking about potential approaches to form validation, session management, and even display logic. This is definitely something worth playing around with.</p>
<p><a href="https://gist.github.com/1254879" target="_blank">All of the code is available as a Gist</a>.</p>
<img src="http://www.robsearles.com/?ak_action=api_record_view&id=529&type=feed" alt="" /><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/RobSearles?a=q07RLDbFcIE:ac_zo19kny4:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/RobSearles?d=yIl2AUoC8zA" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/RobSearles/~4/q07RLDbFcIE" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.robsearles.com/2011/12/07/nodejs-experiments-with-middle-end-part-3/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://www.robsearles.com/2011/12/07/nodejs-experiments-with-middle-end-part-3/</feedburner:origLink></item>
		<item>
		<title>NodeJS: Experiments with Middle End Part 2</title>
		<link>http://feedproxy.google.com/~r/RobSearles/~3/xT07LaiHZmU/</link>
		<comments>http://www.robsearles.com/2011/12/02/nodejs-experiments-with-middle-end-part-2/#comments</comments>
		<pubDate>Fri, 02 Dec 2011 13:48:37 +0000</pubDate>
		<dc:creator>Rob Searles</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[experiments]]></category>
		<category><![CDATA[nodejs]]></category>

		<guid isPermaLink="false">http://www.robsearles.com/?p=519</guid>
		<description><![CDATA[Note:  This is the second in a series of posts describing my experiments with constructing a Middle End. In Part 1, we discussed the concept of the middle end and its advantages.
In my previous post, I talked about Kyle Simpson&#8217;s concept of a Middle End—a layer between the front end and back end. Today, [...]]]></description>
			<content:encoded><![CDATA[<div style="background: #FFFEEB; margin: 10px 0 0 0; padding: 5px 10px; border: 1px solid #ccc;"><strong>Note: </strong> This is the second in a series of posts describing my experiments with constructing a Middle End. In <a href="http://www.robsearles.com/2011/10/20/nodejs-experiments-with-middle-end-part1/">Part 1</a>, we discussed the concept of the middle end and its advantages.</div>
<p>In my previous post, I talked about <a href="http://blog.getify.com/" target="_blank">Kyle Simpson&#8217;s</a> concept of a Middle End—a layer between the front end and back end. Today, I&#8217;m going to sketch out my very simple experiments with constructing such a layer.</p>
<p>What I am focusing on here is simply a means of sharing JavaScript code between the client side and the server side.</p>
<p>Let&#8217;s start with a very simple NodeJS Module, <em>greetings.js</em>:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> Greetings <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #009900;">&#125;</span>
Greetings.<span style="color: #660066;">prototype</span>.<span style="color: #660066;">hello</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>who<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
   <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #3366CC;">&quot;Hello &quot;</span><span style="color: #339933;">+</span>who;
<span style="color: #009900;">&#125;</span>
module.<span style="color: #660066;">exports</span> <span style="color: #339933;">=</span> Greetings;</pre></div></div>

<p>Next, let&#8217;s create a very simple NodeJS script that imports this module, and uses it:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> Greetings <span style="color: #339933;">=</span> require<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'./greetings'</span><span style="color: #009900;">&#41;</span>;
<span style="color: #003366; font-weight: bold;">var</span> greetings <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Greetings<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;
console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span>greetings.<span style="color: #660066;">hello</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'Node'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">+</span><span style="color: #3366CC;">&quot;!&quot;</span><span style="color: #009900;">&#41;</span>;</pre></div></div>

<p>When we run it, this is what we get:</p>

<div class="wp_syntax"><div class="code"><pre class="bash bash" style="font-family:monospace;">$ node test.js
Hello Node<span style="color: #000000; font-weight: bold;">!</span></pre></div></div>

<p>All very simple stuff.</p>
<p>Now, suppose we want to use the Greetings module in our front end web app. </p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;script src=&quot;greetings.js&quot; type=&quot;text/javascript&quot;&gt; &lt;/script&gt;</pre></div></div>

<p>Well, it runs and we get the desired output. However, Firebug reports an error within the greetings.js file:</p>

<div class="wp_syntax"><div class="code"><pre class="text text" style="font-family:monospace;">module is not defined
[Break On This Error] module.exports = Greetings;</pre></div></div>

<p>The solution to this issue is to &#8220;mock up&#8221; the module object so the error isn&#8217;t thrown:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> module <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
   exports<span style="color: #339933;">:</span> undefined
<span style="color: #009900;">&#125;</span>;</pre></div></div>

<p>Run it again, and now we see no errors.</p>
<p>Great, so it&#8217;s working. We can share simple modules between the client and the server sides.</p>
<p>But what if we want to have a more complex module? One that has some dependencies?</p>
<p>That will be discussed in the next post.</p>
<p><a href="https://gist.github.com/1254879" target="_blank">All of the code is available as a Gist</a>.</p>
<img src="http://www.robsearles.com/?ak_action=api_record_view&id=519&type=feed" alt="" /><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/RobSearles?a=xT07LaiHZmU:9iYWzHiQma0:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/RobSearles?d=yIl2AUoC8zA" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/RobSearles/~4/xT07LaiHZmU" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.robsearles.com/2011/12/02/nodejs-experiments-with-middle-end-part-2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://www.robsearles.com/2011/12/02/nodejs-experiments-with-middle-end-part-2/</feedburner:origLink></item>
		<item>
		<title>Arch Linux Virtualbox Update</title>
		<link>http://feedproxy.google.com/~r/RobSearles/~3/e5gWB9UqfMI/</link>
		<comments>http://www.robsearles.com/2011/11/29/arch-linux-virtualbox-update/#comments</comments>
		<pubDate>Tue, 29 Nov 2011 14:07:08 +0000</pubDate>
		<dc:creator>Rob Searles</dc:creator>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[arch]]></category>

		<guid isPermaLink="false">http://www.robsearles.com/?p=306</guid>
		<description><![CDATA[
Gosh! I just found this in my list of draft posts (originally written in early 2010). I thought I might as well post it as it could be useful to someone out there?
Again, this is one of those posts more for me, but hopefully other people might find it useful.
Recently I updated my Arch Linux [...]]]></description>
			<content:encoded><![CDATA[<div style="background: #E0ECF8; margin: 10px 0 0 0; padding: 5px 10px; border: 1px solid #ccc;">
Gosh! I just found this in my list of draft posts (originally written in early 2010). I thought I might as well post it as it could be useful to someone out there?</div>
<p>Again, this is one of those posts more for me, but hopefully other people might find it useful.</p>
<p>Recently I updated my Arch Linux box</p>
<p># pacman -Syu</p>
<p>and during this update a new kernel was added &#8211; which meant that my VirtualBox installation didn&#8217;t work any more, chucking out the following message</p>
<p>The VirtualBox Linux kernel driver (vboxdrv) is either not loaded or there is a permission problem with /dev/vboxdrv. Re-setup the kernel module by executing</p>
<p>&#8216;/etc/init.d/vboxdrv setup&#8217;</p>
<img src="http://www.robsearles.com/?ak_action=api_record_view&id=306&type=feed" alt="" /><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/RobSearles?a=e5gWB9UqfMI:09UkluVG6_g:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/RobSearles?d=yIl2AUoC8zA" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/RobSearles/~4/e5gWB9UqfMI" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.robsearles.com/2011/11/29/arch-linux-virtualbox-update/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://www.robsearles.com/2011/11/29/arch-linux-virtualbox-update/</feedburner:origLink></item>
		<item>
		<title>NodeJS: Experiments with Middle End Part 1</title>
		<link>http://feedproxy.google.com/~r/RobSearles/~3/RG98rLUgg6E/</link>
		<comments>http://www.robsearles.com/2011/10/20/nodejs-experiments-with-middle-end-part1/#comments</comments>
		<pubDate>Thu, 20 Oct 2011 21:33:39 +0000</pubDate>
		<dc:creator>Rob Searles</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[experiments]]></category>
		<category><![CDATA[middle-end]]></category>
		<category><![CDATA[nodejs]]></category>

		<guid isPermaLink="false">http://www.robsearles.com/?p=515</guid>
		<description><![CDATA[Back in October last year (2010) a bunch of us fled Berlin and made for Warsaw. Not just for the Vodka you understand, but more specifically for the Front-Trends conference.
One of the talks was discussing the concept of the Middle End, by Kyle Simpson. This really made an impression on me, and I think I [...]]]></description>
			<content:encoded><![CDATA[<p>Back in October last year (2010) a bunch of us fled Berlin and made for Warsaw. Not just for the Vodka you understand, but more specifically for the Front-Trends conference.</p>
<p>One of the talks was discussing the concept of the <a href="http://blog.getify.com/2010/07/what-exactly-is-the-middle-end/" target="_blank">Middle End, by Kyle Simpson</a>. This really made an impression on me, and I think I probably bored Kyle in the bar afterwards with my drunken enthusing. When we arrived back home, my aim was to start playing around with the idea of a Middle End as soon as possible. Unfortunately, as is life, events took over (such as discovering one is going to become a dad!) and these thoughts were consigned to the back of my mind.</p>
<p>Until now</p>
<p><span id="more-515"></span></p>
<p>The more I hack around with NodeJS the more I&#8217;ve been able to experiment with different aspects of it. Well, today I began hacking around with some Middle End concepts.</p>
<p>The idea of the Middle End is quite simple.</p>
<p>In a web application there is traditionally the Front End and the Back End. The front end handles all of the client side processing (such as rendering some whizzy JQuery plugins or whatever). The back end handles the actual hard crunching, such as retrieving and adding data from the storage engine.</p>
<p>However, there are certain aspects that both the front end <strong>and</strong> the back end need to handle. Some things that spring to mind are: session managment, input validation and possibly some display logic (Kyle&#8217;s post goes into some more depth here).</p>
<p>Currently these aspects are handled both by the front end and back end code, but as traditionally front end code is JavaScript where as back end code maybe Ruby, PHP, Python, etc that means there ends up being duplication of code. Two different languages doing the same thing.</p>
<p>This is in direct violation of the DRY principal. Quite simply, this is a waste.</p>
<p>There is a solution to this waste. Using JavaScript on the server side, effectively code can now be reused. But how can you use the same piece of code for the client side as well as the server side? In my next post I&#8217;ll be looking at how to do that.</p>
<img src="http://www.robsearles.com/?ak_action=api_record_view&id=515&type=feed" alt="" /><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/RobSearles?a=RG98rLUgg6E:YUmepmgMXmM:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/RobSearles?d=yIl2AUoC8zA" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/RobSearles/~4/RG98rLUgg6E" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.robsearles.com/2011/10/20/nodejs-experiments-with-middle-end-part1/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		<feedburner:origLink>http://www.robsearles.com/2011/10/20/nodejs-experiments-with-middle-end-part1/</feedburner:origLink></item>
		<item>
		<title>Note to self: Node Deployment</title>
		<link>http://feedproxy.google.com/~r/RobSearles/~3/BTyjocpzUvY/</link>
		<comments>http://www.robsearles.com/2011/10/07/note-to-self-node-deployment/#comments</comments>
		<pubDate>Fri, 07 Oct 2011 17:37:46 +0000</pubDate>
		<dc:creator>Rob Searles</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[nodejs]]></category>

		<guid isPermaLink="false">http://www.robsearles.com/?p=546</guid>
		<description><![CDATA[This is really a note to me as since Delicious has &#8220;jumped the shark&#8221; I don&#8217;t really know where else to put it.
I&#8217;ve been researching automated deployment of NodeJS applications, and found this excellent blog post to help me out:
Deploying Node.JS Apps, by Ben Gourley.
Plus this question on stack overflow on nginx proxying should help [...]]]></description>
			<content:encoded><![CDATA[<p>This is really a note to me as since Delicious has &#8220;jumped the shark&#8221; I don&#8217;t really know where else to put it.</p>
<p>I&#8217;ve been researching automated deployment of NodeJS applications, and found this excellent blog post to help me out:</p>
<p><a href="http://blog.clock.co.uk/2011/04/11/deploying-node-js-apps/" target="_blank">Deploying Node.JS Apps, by Ben Gourley</a>.</p>
<p>Plus <a href="http://stackoverflow.com/questions/5009324/node-js-nginx-and-now" target="_blank">this question on stack overflow on nginx proxying</a> should help me out.</p>
<p>That&#8217;s it. now I just need some time to play around with this stuff!</p>
<p>Also, if anyone knows of a really simple, easy to organize bookmarking site please let me know!</p>
<img src="http://www.robsearles.com/?ak_action=api_record_view&id=546&type=feed" alt="" /><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/RobSearles?a=BTyjocpzUvY:4R4y2CT2eNA:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/RobSearles?d=yIl2AUoC8zA" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/RobSearles/~4/BTyjocpzUvY" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.robsearles.com/2011/10/07/note-to-self-node-deployment/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://www.robsearles.com/2011/10/07/note-to-self-node-deployment/</feedburner:origLink></item>
		<item>
		<title>JSConf EU – Day 1</title>
		<link>http://feedproxy.google.com/~r/RobSearles/~3/Yu30b7SEG0I/</link>
		<comments>http://www.robsearles.com/2011/10/01/jsconf-eu-day-1/#comments</comments>
		<pubDate>Sat, 01 Oct 2011 19:05:06 +0000</pubDate>
		<dc:creator>Rob Searles</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[jsconf]]></category>

		<guid isPermaLink="false">http://www.robsearles.com/?p=542</guid>
		<description><![CDATA[Back home now after a busy day at JSConf EU, brain is pretty melted due to a lot of good talks, so will make this a quick one.
Three things really stood out for me today.
Firstly, Cloud9 IDE. This is a browser based JavaScript IDE, and it looks fantastic. With no setup or configuration needed it [...]]]></description>
			<content:encoded><![CDATA[<p>Back home now after a busy day at <a href="http://jsconf.eu/2011/" target="_blank">JSConf EU</a>, brain is pretty melted due to a lot of good talks, so will make this a quick one.</p>
<p>Three things really stood out for me today.</p>
<p>Firstly, <a href="http://c9.io/" target="_blank">Cloud9 IDE</a>. This is a browser based JavaScript IDE, and it looks fantastic. With no setup or configuration needed it lets you just start writing NodeJS directly from your browser. The lovely guys even gave me a premium account free for 1 year! I am going to sign up and start hacking away as soon as I get a chance.</p>
<p>Secondly, even though the Arduino talk was a bit, er, ramshackled, they did talk about the <a href="http://www.raspberrypi.org/" target="_blank">Raspberry Pi</a>. This is a tiny (and I mean tiny) ARM box running Linux. It should be available in November some time, which will be perfect timing for a birthday present to myself!</p>
<p>Finally, <a href="https://github.com/kripken/emscripten" target="_blank">Emscripten</a> is very exciting. Emscripten is a compiler that takes bitcode and turns it into JavaScript. From what I understood, this is what made <a href="http://repl.it" target="_blank">repl.it</a> possible, which I saw <a href="http://news.ycombinator.com/item?id=3056490" target="_blank">yesterday in Hacker News</a> and thought was so cool I bookmarked it!</p>
<p>As there were two tracks I did miss a bunch of talks, but I&#8217;m sure they will be availble on the <a href="http://jsconf.eu/2011/" target="_blank">website</a> soon.</p>
<p>All in all, good stuff, tiring (especially as some little madam decided to wake up at 5am) but brain is now firing off with a bunch of possibilities!</p>
<img src="http://www.robsearles.com/?ak_action=api_record_view&id=542&type=feed" alt="" /><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/RobSearles?a=Yu30b7SEG0I:qu4qnkhn5yA:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/RobSearles?d=yIl2AUoC8zA" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/RobSearles/~4/Yu30b7SEG0I" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.robsearles.com/2011/10/01/jsconf-eu-day-1/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://www.robsearles.com/2011/10/01/jsconf-eu-day-1/</feedburner:origLink></item>
		<item>
		<title>JSConf.eu Tomorrow = Huzzah!</title>
		<link>http://feedproxy.google.com/~r/RobSearles/~3/3rbqoP1Agpo/</link>
		<comments>http://www.robsearles.com/2011/09/30/jsconf-eu-tomorrow-huzzah/#comments</comments>
		<pubDate>Fri, 30 Sep 2011 21:56:35 +0000</pubDate>
		<dc:creator>Rob Searles</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[jsconf]]></category>

		<guid isPermaLink="false">http://www.robsearles.com/?p=538</guid>
		<description><![CDATA[This is just a quick post as I&#8217;m off to bed for an early night&#8217;s sleep. JSConf EU is tomorrow and am one of the lucky few who have in their posession a &#8220;golden ticket&#8221; to attend. It&#8217;s  a jam-packed event starting at an unaturally early 8am! Do they know this is Berlin?! You can&#8217;t [...]]]></description>
			<content:encoded><![CDATA[<p>This is just a quick post as I&#8217;m off to bed for an early night&#8217;s sleep. <a href="http://jsconf.eu/2011/" target="_blank">JSConf EU</a> is tomorrow and am one of the lucky few who have in their posession a &#8220;golden ticket&#8221; to attend. It&#8217;s  a jam-packed event starting at an unaturally early 8am! Do they know this is Berlin?! You can&#8217;t even buy breakfast at 8am on a Saturday here!</p>
<p>I have high hopes for the conference. Last year I didn&#8217;t even attend the main event, just the pre-party on the Friday night and still came away with a load of ideas! One conversation lead me to completely rewrite major parts of the <a href="http://www.jabbakam.com">Jabbakam</a> system. Who knows what new thoughts will spark in my head when I actually go to the real thing?</p>
<p>For updates, <a href="http://twitter.com/ibrow" target="_blank">follow me on Twitter</a>.</p>
<img src="http://www.robsearles.com/?ak_action=api_record_view&id=538&type=feed" alt="" /><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/RobSearles?a=3rbqoP1Agpo:YWmQCuc-nBI:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/RobSearles?d=yIl2AUoC8zA" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/RobSearles/~4/3rbqoP1Agpo" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.robsearles.com/2011/09/30/jsconf-eu-tomorrow-huzzah/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://www.robsearles.com/2011/09/30/jsconf-eu-tomorrow-huzzah/</feedburner:origLink></item>
		<item>
		<title>NodeJS: Experiments with Processes</title>
		<link>http://feedproxy.google.com/~r/RobSearles/~3/8rtPznYWY2k/</link>
		<comments>http://www.robsearles.com/2011/09/28/nodejs-experiments-with-processes/#comments</comments>
		<pubDate>Wed, 28 Sep 2011 13:34:19 +0000</pubDate>
		<dc:creator>Rob Searles</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[nodejs]]></category>

		<guid isPermaLink="false">http://www.robsearles.com/?p=500</guid>
		<description><![CDATA[Experimenting with Node JS daemon processes which spawn multiple children.]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been hacking about with NodeJS a lot recently, and lately my attention has turned towards spawning child processes. Luckily NodeJS makes this very easy to do, and also the documentation is pretty good.</p>
<p>What I have been working on is having a single parent process that runs constantly in the background, and this parent is responsible for child processes which actually do the work. One thing I found particularly interesting was how the parent can handle a child crashing, and also what happens to the children when the parent is killed. Below is the culmination of my hackings, it comprises of two simple scripts, a child script, and a parent. I have <a href="https://github.com/ibrow/RS-experiment-node-process" target="_blank">committed them to my GitHub account</a>, so feel free to clone, fork, hack or whatever.</p>
<p><span id="more-500"></span></p>
<p>Note: Node version at time of writing was v0.4.9 and running off a Debian machine.</p>
<h3>Child Worker</h3>
<p>For this experiment we need a really simple child script. It doesn&#8217;t have to do anything apart from tell us it is alive when it is running, and then kill itself so we can see how the parent handles this event.</p>
<p>The main bulk of this script is the loop:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> child_loop <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
   <span style="color: #006600; font-style: italic;">// report the current loop</span>
   console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Child loop: &quot;</span><span style="color: #339933;">+</span>NUMBER_OF_LOOPS<span style="color: #009900;">&#41;</span>;
&nbsp;
   <span style="color: #006600; font-style: italic;">// to simulate the child exiting itself without just running</span>
   <span style="color: #006600; font-style: italic;">// out of program, if we have completed the desired number of</span>
   <span style="color: #006600; font-style: italic;">// loops, exit this process</span>
   <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span> NUMBER_OF_LOOPS <span style="color: #339933;">&amp;</span>lt; 0 <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Child finished loop&quot;</span><span style="color: #009900;">&#41;</span>;
      process.<span style="color: #660066;">exit</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;
   <span style="color: #009900;">&#125;</span>
&nbsp;
   <span style="color: #006600; font-style: italic;">// sleep for a bit, then continue through the loops</span>
   setTimeout<span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> child_loop<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> LOOP_SLEEP_TIME<span style="color: #009900;">&#41;</span>;
   NUMBER_OF_LOOPS--;
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// start the loop</span>
child_loop<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;</pre></div></div>

<p>As you can see this loops for NUMBER_OF_LOOPS, outputting what loop it is currently on. At the end of the loop it kills itself, informing us whilst it does it.</p>
<p>So, this is fine whilst the child is running, but we also need to try and get the child to clean up after itself if it is killed by an external process &#8211; i.e. the parent. After some playing around and reading of the documentation, I ended up with this:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// detect and report if this child exited</span>
process.<span style="color: #660066;">on</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;exit&quot;</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
   console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Child exiting&quot;</span><span style="color: #009900;">&#41;</span>;
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;
<span style="color: #006600; font-style: italic;">// detect and report if this child was killed</span>
process.<span style="color: #660066;">on</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;SIGTERM&quot;</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
   console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Child SIGTERM detected&quot;</span><span style="color: #009900;">&#41;</span>;
   process.<span style="color: #660066;">exit</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;</pre></div></div>

<p>I need to go more in depth, but whilst testing, this seemed to correctly handle the child being terminated externally.</p>
<p>So, that&#8217;s the child script. You can run this directly from the command line and it does what is expected. Good, so we&#8217;re all ready to build the parent so it can spawn multiple children.</p>
<h3>Parent</h3>
<p>This was the main focus of the experiment. The chief concerns were:</p>
<ul>
<li>Ensuring the process ran indefinitely as a daemon</li>
<li>Spawning the children</li>
<li>Handling a child that crashed or exited</li>
<li>Cleaning up the children when the parent was exited</li>
</ul>
<p>The first point is relatively simple, all we need is an indefinite loop:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript javascript" style="font-family:monospace;">loop <span style="color: #339933;">=</span> setInterval<span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
   <span style="color: #006600; font-style: italic;">// functionality here</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> LOOP_SLEEP_TIME<span style="color: #009900;">&#41;</span>;</pre></div></div>

<p>Handling the children was a little more complex. In the end I created an array that would contain the children. Within the main loop, I then ran through that array, checking to see if the child existed, if not then spawning it. Notice how the loop is returning itself into the variable &#8220;loop&#8221;. This will be important later.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> spawn <span style="color: #339933;">=</span> require<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'child_process'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">spawn</span>;
<span style="color: #003366; font-weight: bold;">var</span> loop <span style="color: #339933;">=</span> setInterval<span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
   <span style="color: #000066; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span>i<span style="color: #339933;">=</span>0; i</pre></div></div>

<p>This is fine for creating the children, but then what if a child dies? Simple really. The parent needs to listen for the <strong>exit</strong> event on the child. When detected, ensure that the array element for that child is cleaned out, and then in the next loop, the child will be respawned.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript javascript" style="font-family:monospace;">children<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">on</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'exit'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span>code<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
   children<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> undefined;
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;</pre></div></div>

<p>Next, what if the parent is killed. We need to make sure we don&#8217;t leave any orphaned children behind. After some playing around, I found that two events needed to be listened for. Again, the <strong>exit</strong> event, this time on parent, but also the <strong>SIGTERM</strong> event on the parent.</p>
<p>When a <strong>SIGTERM</strong> event is detected, we simply tell the script to exit cleanly:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript javascript" style="font-family:monospace;">process.<span style="color: #660066;">on</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;SIGTERM&quot;</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
   console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Parent SIGTERM detected&quot;</span><span style="color: #009900;">&#41;</span>;
   <span style="color: #006600; font-style: italic;">// exit cleanly</span>
   process.<span style="color: #660066;">exit</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;</pre></div></div>

<p>Now, when the <strong>exit</strong> event is triggered, we can clean up the children. One thing we must also do, is stop the Interval of the main loop. If we didn&#8217;t do this, when the children were killed, the next &#8220;tick&#8221; of the loop, then they would all be created again. It&#8217;s possible that this could happen before the main script has exited, orphaning the children. We can eaily do this as we have the variable that the setInterval call has been saved into.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript javascript" style="font-family:monospace;">process.<span style="color: #660066;">on</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;exit&quot;</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
   <span style="color: #006600; font-style: italic;">// ensure no more &quot;ticks&quot;</span>
   clearInterval<span style="color: #009900;">&#40;</span>loop<span style="color: #009900;">&#41;</span>;
&nbsp;
   <span style="color: #006600; font-style: italic;">// kill all children!</span>
   <span style="color: #000066; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span>i<span style="color: #339933;">=</span>0; i<span style="color: #339933;">&lt;</span>TOTAL_CHILDREN; i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>children<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span> <span style="color: #339933;">!=</span> undefined<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
         console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Kill child &quot;</span><span style="color: #339933;">+</span>i<span style="color: #009900;">&#41;</span>;
         children<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">kill</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;
      <span style="color: #009900;">&#125;</span>
   <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;</pre></div></div>

<p>That is it for the main bulk of the script. As I&#8217;m testing and running it on Debian I am using <a href="#">start-stop-daemon</a> to run the parent as a daemon. As such I&#8217;ve had to add the functionality for creating a .pid file. One issue I had with this, and I have no idea why, is when I tried to write the <strong>process.pid</strong> value to a file it filled the file up with nonsense. However, if I converted it to a string (which from what I can tell it already was) before writing it to the file, then it worked fine.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> pid <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;&quot;</span><span style="color: #339933;">+</span>process.<span style="color: #660066;">pid</span>; <span style="color: #006600; font-style: italic;">// need to turn into a string</span>
fs.<span style="color: #660066;">writeFile</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'/tmp/parent.pid'</span><span style="color: #339933;">,</span> pid<span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span>err<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
   <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>err<span style="color: #009900;">&#41;</span> <span style="color: #000066; font-weight: bold;">throw</span> err;
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;</pre></div></div>

<p>You can see the complete parent and child scripts on my Github repository.</p>
<h3>Running</h3>
<p>Now we have our scripts, we need to run them to see if they actually behave as expected. In Debian (and derivatives) this is pretty simple:</p>
<p>To start</p>

<div class="wp_syntax"><div class="code"><pre class="bash bash" style="font-family:monospace;">start-stop-daemon <span style="color: #660033;">--start</span> <span style="color: #660033;">--exec</span> <span style="color: #000000; font-weight: bold;">/</span>home<span style="color: #000000; font-weight: bold;">/</span>rob<span style="color: #000000; font-weight: bold;">/</span>workspace<span style="color: #000000; font-weight: bold;">/</span>NodeJS<span style="color: #000000; font-weight: bold;">/</span>process<span style="color: #000000; font-weight: bold;">/</span>parent.js <span style="color: #660033;">--pidfile</span> <span style="color: #000000; font-weight: bold;">/</span>tmp<span style="color: #000000; font-weight: bold;">/</span>parent.js.pid</pre></div></div>

<p>And to stop</p>

<div class="wp_syntax"><div class="code"><pre class="bash bash" style="font-family:monospace;">start-stop-daemon <span style="color: #660033;">--stop</span> <span style="color: #660033;">--pidfile</span> <span style="color: #000000; font-weight: bold;">/</span>tmp<span style="color: #000000; font-weight: bold;">/</span>parent.js.pid</pre></div></div>

<p>As we are not running the process in the background then you will need to have two terminal windows open. Also, you will be able to see the output which explains what is going on.</p>
<p>You should be able to easily convert the script to log to a file (I recommend <a href="https://github.com/visionmedia/log.js">Vision Media&#8217;s Log.js</a>) and then write an init.d script using the <strong>start-stop-daemon</strong> functionality to create a background daemon process .</p>
<p>You should be able to see that we now have the ability to run a Node.JS daemon process which can spawn multiple children, ensuring it can keep a consistent number of children available, and then handle the cleanup if that daemon process is killed. This opens up lots of possibilities!</p>
<p>Again, feel free to <a href="https://github.com/ibrow/RS-experiment-node-process" target="_blank">clone, fork, hack, improve, etc</a> the code for this.</p>
<img src="http://www.robsearles.com/?ak_action=api_record_view&id=500&type=feed" alt="" /><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/RobSearles?a=8rtPznYWY2k:mK7OPOh5v_Y:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/RobSearles?d=yIl2AUoC8zA" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/RobSearles/~4/8rtPznYWY2k" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.robsearles.com/2011/09/28/nodejs-experiments-with-processes/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		<feedburner:origLink>http://www.robsearles.com/2011/09/28/nodejs-experiments-with-processes/</feedburner:origLink></item>
	</channel>
</rss>

