<?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>Rob Searles &#187; Tutorials</title>
	<atom:link href="http://www.robsearles.com/category/tutorials/feed/" rel="self" type="application/rss+xml" />
	<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>
			<item>
		<title>NodeJS: Experiments with Middle End Part 3</title>
		<link>http://www.robsearles.com/2011/12/07/nodejs-experiments-with-middle-end-part-3/</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="" />]]></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>
		</item>
		<item>
		<title>NodeJS: Experiments with Middle End Part 2</title>
		<link>http://www.robsearles.com/2011/12/02/nodejs-experiments-with-middle-end-part-2/</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="" />]]></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>
		</item>
		<item>
		<title>NodeJS: Experiments with Processes</title>
		<link>http://www.robsearles.com/2011/09/28/nodejs-experiments-with-processes/</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="" />]]></content:encoded>
			<wfw:commentRss>http://www.robsearles.com/2011/09/28/nodejs-experiments-with-processes/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Amazon S3: easily bulk delete buckets</title>
		<link>http://www.robsearles.com/2011/05/11/amazon-s3-easily-bulk-delete-buckets/</link>
		<comments>http://www.robsearles.com/2011/05/11/amazon-s3-easily-bulk-delete-buckets/#comments</comments>
		<pubDate>Wed, 11 May 2011 13:19:47 +0000</pubDate>
		<dc:creator>Rob Searles</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[aws]]></category>
		<category><![CDATA[ec2]]></category>

		<guid isPermaLink="false">http://www.robsearles.com/?p=466</guid>
		<description><![CDATA[If you need to delete your S3 buckets but they contain hundreds, thousands or even millions of images, then fire up an instance and run s3nukem.]]></description>
			<content:encoded><![CDATA[<p>With <a href="http://www.jabbakam.com" target="_blank">Jabbakam</a> we save a <strong>lot</strong> of images. All these images are stored in Amazon&#8217;s S3. Recently I thought I&#8217;d delete some of the test buckets.</p>
<p>Not as easy as I first thought. It turns out that you have to empty buckets before you can delete them. Fair enough, but there didn&#8217;t seem to be a way to easily bulk delete hundreds of images.</p>
<p>After some Googling I found <a href="https://github.com/lathanh/s3nukem" target="_blank">Robert LaThanh&#8217;s S3Nukem</a> which looked like it would do the job. All I needed to do now was fire up an EC2 instance, install and run. The steps were as follows:</p>
<ol>
<li>Create an EC2 instance on AWS (I used Ubuntu on a medium instance)</li>
<li>make sure it is all up-to-date

<div class="wp_syntax"><div class="code"><pre class="bash bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">apt-get</span> update
<span style="color: #c20cb9; font-weight: bold;">apt-get</span> upgrade</pre></div></div>

</li>
<li>install ruby (if not already installed)

<div class="wp_syntax"><div class="code"><pre class="bash bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">apt-get</span> <span style="color: #c20cb9; font-weight: bold;">install</span> ruby1.8 ruby1.8-dev</pre></div></div>

<p>(not sure about ruby1.8-dev, but added just in case)</li>
<li>install Ruby Gems

<div class="wp_syntax"><div class="code"><pre class="bash bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">apt-get</span> <span style="color: #c20cb9; font-weight: bold;">install</span> rubygems</pre></div></div>

</li>
<li>install right_aws and s3nukem

<div class="wp_syntax"><div class="code"><pre class="bash bash" style="font-family:monospace;">gem <span style="color: #c20cb9; font-weight: bold;">install</span> right_aws
<span style="color: #7a0874; font-weight: bold;">cd</span> <span style="color: #000000; font-weight: bold;">/</span>tmp
<span style="color: #c20cb9; font-weight: bold;">wget</span> http:<span style="color: #000000; font-weight: bold;">//</span>github.com<span style="color: #000000; font-weight: bold;">/</span>lathanh<span style="color: #000000; font-weight: bold;">/</span>s3nukem<span style="color: #000000; font-weight: bold;">/</span>raw<span style="color: #000000; font-weight: bold;">/</span>master<span style="color: #000000; font-weight: bold;">/</span>s3nukem <span style="color: #660033;">--no-check-certificate</span>
<span style="color: #c20cb9; font-weight: bold;">chmod</span> +x s3nukem</pre></div></div>

</li>
<li>run

<div class="wp_syntax"><div class="code"><pre class="bash bash" style="font-family:monospace;">.<span style="color: #000000; font-weight: bold;">/</span>s3nukem <span style="color: #660033;">-t</span> <span style="color: #000000;">20</span> <span style="color: #660033;">-a</span>  <span style="color: #660033;">-s</span></pre></div></div>

</li>
</ol>
<p>Several hours and over 15 million deleted images later it was all done and I shut down the instance.</p>
<img src="http://www.robsearles.com/?ak_action=api_record_view&id=466&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://www.robsearles.com/2011/05/11/amazon-s3-easily-bulk-delete-buckets/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Awesome WM Clicky Widget</title>
		<link>http://www.robsearles.com/2011/04/06/awesome-wm-clicky-widget/</link>
		<comments>http://www.robsearles.com/2011/04/06/awesome-wm-clicky-widget/#comments</comments>
		<pubDate>Wed, 06 Apr 2011 14:02:03 +0000</pubDate>
		<dc:creator>Rob Searles</dc:creator>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[awesome]]></category>
		<category><![CDATA[lua]]></category>

		<guid isPermaLink="false">http://www.robsearles.com/?p=420</guid>
		<description><![CDATA[After a couple of hours playing around with Awesome Window Manager, Lua and the Clicky API I hack together a Clicky widget for Awesome.  ]]></description>
			<content:encoded><![CDATA[<p><img class="alignright size-full wp-image-444" title="Awesome WM Clicky Widget" src="http://www.robsearles.com/wp-content/uploads/2011/04/2011-04-07-130830_266x105_scrot.png" alt="Awesome WM Clicky Widget" width="266" height="105" />As some of you know, whilst Emacs is my editor (<a href="http://en.wiktionary.org/wiki/et_al." target="_blank">et al.</a>) of choice my favourite window manger is <a href="http://awesome.naquadah.org/" target="_blank">Awesome</a>. Awesome is, according to the website,</p>
<blockquote><p>.. extremely fast, small, dynamic and <strong>heavily extensible</strong> using the <strong><a style="color: #347272;" href="http://www.lua.org/">Lua</a> programming language</strong></p></blockquote>
<p>I&#8217;ve never delved into Lua before, but since using Awesome I&#8217;ve had to play around with a bunch of config files. They have always looked very clean and simple so, this weekend, with a spare couple of hours to kill I thought I&#8217;d have a play around with Lua and see if Awesome really is as &#8220;heavily extensible&#8221; as the claims make out.</p>
<p>A couple of weeks ago I set up real time web analytics on a number of sites with <a href="http://getclicky.com/" target="_blank">Clicky</a>. Clicky have an <a href="http://getclicky.com/help/api" target="_blank">incredibly simple API</a> which I thought would be perfect to play around with.</p>
<p>I decided to try and build a widget that displays the total visitors and actions for a specified website. Really simple, but also quite useful. I had never written anything in Lua before, or even played around with widgets too much in Awesome. Fortunately both Awesome and Lua made the whole thing fantastically easy and fun. What follows is the code I wrote, use it if you want to, it&#8217;s just an example of what I did with my first attempt, so feel free to extend it as you want.</p>
<p><span id="more-420"></span></p>
<p>First, include the code below in a file called &#8220;clicky.lua&#8221; in your <em>~/.config/awesome</em> directory. Note, you will also need to add the <em>dkjson.lua</em> file there too, which you can find at <a href="http://www-users.rwth-aachen.de/David.Kolf/json-lua">http://www-users.rwth-aachen.de/David.Kolf/json-lua</a>.</p>

<div class="wp_syntax"><div class="code"><pre class="lua lua" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">-- we need the dkjson library</span>
<span style="color: #808080; font-style: italic;">-- can be found here:</span>
<span style="color: #808080; font-style: italic;">-- http://www-users.rwth-aachen.de/David.Kolf/json-lua</span>
json <span style="color: #66cc66;">=</span> <span style="color: #b1b100;">require</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;dkjson&quot;</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">-- Site ID and Key for Clicky</span>
site_id <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">&quot;[YOUR-CLICKY-SITE-ID]&quot;</span>
site_key <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">&quot;[YOUR-CLICKY-SITE-KEY]&quot;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">-- the clicky object</span>
Clicky <span style="color: #66cc66;">=</span> <span style="color: #66cc66;">&#123;</span>
   actions <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">&quot;n/a&quot;</span>,
   visitors <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">&quot;n/a&quot;</span>,
&nbsp;
   update <span style="color: #66cc66;">=</span> <span style="color: #b1b100;">function</span><span style="color: #66cc66;">&#40;</span>self, widget<span style="color: #66cc66;">&#41;</span>
               j <span style="color: #66cc66;">=</span> self:get_json<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
               self:parse_json<span style="color: #66cc66;">&#40;</span>j<span style="color: #66cc66;">&#41;</span>
               widget.text <span style="color: #66cc66;">=</span> Clicky.visitors .. <span style="color: #ff0000;">&quot;/&quot;</span> .. Clicky.actions
            <span style="color: #b1b100;">end</span>,
&nbsp;
   <span style="color: #808080; font-style: italic;">-- call the Clicky API, expecting JSON in return</span>
   get_json <span style="color: #66cc66;">=</span> <span style="color: #b1b100;">function</span><span style="color: #66cc66;">&#40;</span>self<span style="color: #66cc66;">&#41;</span>
               clicky_url <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">&quot;http://api.getclicky.com/api/stats/4?site_id=&quot;</span>..site_id..<span style="color: #ff0000;">&quot;&amp;amp;sitekey=&quot;</span>..site_key..<span style="color: #ff0000;">&quot;&amp;amp;type=visitors,actions&amp;amp;date=today&amp;amp;output=json&quot;</span>
               <span style="color: #808080; font-style: italic;">-- get the visitors and actions for today</span>
               command <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">'curl -s &quot;'</span>..clicky_url..<span style="color: #ff0000;">'&quot;'</span>
               f <span style="color: #66cc66;">=</span> <span style="color: #b1b100;">io</span>.popen<span style="color: #66cc66;">&#40;</span>command<span style="color: #66cc66;">&#41;</span>
               <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">not</span> f<span style="color: #66cc66;">&#41;</span> <span style="color: #b1b100;">then</span> <span style="color: #b1b100;">return</span> 0 <span style="color: #b1b100;">end</span>
               <span style="color: #808080; font-style: italic;">-- store the returned json as a string</span>
               <span style="color: #b1b100;">return</span> f:<span style="color: #b1b100;">read</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;*all&quot;</span><span style="color: #66cc66;">&#41;</span>
            <span style="color: #b1b100;">end</span>,
&nbsp;
   <span style="color: #808080; font-style: italic;">-- parse the JSON from Clicky</span>
   parse_json <span style="color: #66cc66;">=</span> <span style="color: #b1b100;">function</span><span style="color: #66cc66;">&#40;</span>self, j<span style="color: #66cc66;">&#41;</span>
                   <span style="color: #b1b100;">local</span> obj, pos, err <span style="color: #66cc66;">=</span> json.decode <span style="color: #66cc66;">&#40;</span>j, <span style="color: #cc66cc;">1</span>, <span style="color: #b1b100;">nil</span><span style="color: #66cc66;">&#41;</span>
                   <span style="color: #b1b100;">if</span> err <span style="color: #b1b100;">then</span>
                      self.actions <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">&quot;e&quot;</span>
                      self.visitors <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">&quot;e&quot;</span>
                   <span style="color: #b1b100;">else</span>
                      <span style="color: #b1b100;">for</span> k,v <span style="color: #b1b100;">in</span> <span style="color: #b1b100;">pairs</span><span style="color: #66cc66;">&#40;</span>obj<span style="color: #66cc66;">&#41;</span> <span style="color: #b1b100;">do</span>
                         <span style="color: #b1b100;">if</span> v<span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">&quot;type&quot;</span><span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">==</span> <span style="color: #ff0000;">&quot;actions&quot;</span> <span style="color: #b1b100;">then</span>
                            self.actions <span style="color: #66cc66;">=</span> v<span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">&quot;dates&quot;</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">&quot;items&quot;</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">&quot;value&quot;</span><span style="color: #66cc66;">&#93;</span>
                         <span style="color: #b1b100;">end</span>
                         <span style="color: #b1b100;">if</span> v<span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">&quot;type&quot;</span><span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">==</span> <span style="color: #ff0000;">&quot;visitors&quot;</span> <span style="color: #b1b100;">then</span>
                            self.visitors <span style="color: #66cc66;">=</span> v<span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">&quot;dates&quot;</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">&quot;items&quot;</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">&quot;value&quot;</span><span style="color: #66cc66;">&#93;</span>
                         <span style="color: #b1b100;">end</span>
                      <span style="color: #b1b100;">end</span>
                   <span style="color: #b1b100;">end</span>
                <span style="color: #b1b100;">end</span>
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">-- init the widget</span>
myclickystats <span style="color: #66cc66;">=</span> widget<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#123;</span> <span style="color: #b1b100;">type</span> <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">&quot;textbox&quot;</span>, name <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">&quot;clicky&quot;</span>, align <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">&quot;right&quot;</span> <span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>
myclickystats.text <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">&quot;Clicky&quot;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">-- get the initial update</span>
Clicky:update<span style="color: #66cc66;">&#40;</span>myclickystats<span style="color: #66cc66;">&#41;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">-- now poll every 15 minutes</span>
minutes <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">15</span>
myclickytimer <span style="color: #66cc66;">=</span> timer<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#123;</span> timeout <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">60</span> <span style="color: #66cc66;">*</span> minutes <span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>
myclickytimer:add_signal<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;timeout&quot;</span>, <span style="color: #b1b100;">function</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> Clicky:update<span style="color: #66cc66;">&#40;</span>myclickystats<span style="color: #66cc66;">&#41;</span> <span style="color: #b1b100;">end</span><span style="color: #66cc66;">&#41;</span>
myclickytimer:start<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>Now, in your <em>~/.config/awesome/rc.lua</em> file you need to insert two snippets.</p>
<p>Towards the top of the file, add</p>

<div class="wp_syntax"><div class="code"><pre class="lua lua" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">-- Include my Clicky stats</span>
<span style="color: #b1b100;">require</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;clicky&quot;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>and within the screen loop add the <strong>myclickystats</strong> widget</p>

<div class="wp_syntax"><div class="code"><pre class="lua lua" style="font-family:monospace;"><span style="color: #b1b100;">for</span> s <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">1</span>, screen.count<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #b1b100;">do</span>
   ...
    <span style="color: #808080; font-style: italic;">-- Add widgets to the wibox - order matters</span>
    mywibox<span style="color: #66cc66;">&#91;</span>s<span style="color: #66cc66;">&#93;</span>.widgets <span style="color: #66cc66;">=</span> <span style="color: #66cc66;">&#123;</span>
        ...
        s <span style="color: #66cc66;">==</span> <span style="color: #cc66cc;">1</span> <span style="color: #b1b100;">and</span> myclickystats <span style="color: #b1b100;">or</span> <span style="color: #b1b100;">nil</span>,
        ...
    <span style="color: #66cc66;">&#125;</span>
<span style="color: #b1b100;">end</span></pre></div></div>

<p>and that should be it. Restart awesome with <strong>&lt;Super&gt; &#8211; &lt;Ctrl&gt; &#8211; r</strong> and if you have done everything right you should see something like x/y in the top right of your screen. (Disclaimer: the above setup works for me on Ubuntu 9.10 and Arch, but if Awesome doesn&#8217;t restart you&#8217;re on your own!)</p>
<p>After building this simple widget I have learnt a lot about both Lua and Awesome.</p>
<p>On this, my first impression of Lua, it seems very simple and clean but also probably quite powerful, something I&#8217;ll definitely play with more.</p>
<p><em>Update: added screenshot after <a href="http://twitter.com/getclicky/status/55725963979390978" target="_blank">tweet from @getclicky</a>.</em></p>
<img src="http://www.robsearles.com/?ak_action=api_record_view&id=420&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://www.robsearles.com/2011/04/06/awesome-wm-clicky-widget/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>NodeJS Tutorial &#8211; Part 2, Routing</title>
		<link>http://www.robsearles.com/2010/05/31/nodejs-tutorial-part-2-routing/</link>
		<comments>http://www.robsearles.com/2010/05/31/nodejs-tutorial-part-2-routing/#comments</comments>
		<pubDate>Mon, 31 May 2010 15:26:45 +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=352</guid>
		<description><![CDATA[Note: This is the second part of my NodeJS tutorial. You may want to go back to the first part of this tutorial if you haven&#8217;t already read it.
In the first part of this tutorial we looked at reading a document from CouchDB and keeping our markup separate from our code using Haml templates. We arrived [...]]]></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 part of my <a href="http://www.robsearles.com/2010/05/28/nodejs-tutorial-with-couchdb-and-haml-erdnodeflips/">NodeJS tutorial</a>. You may want to go back to the <a href="http://www.robsearles.com/2010/05/28/nodejs-tutorial-with-couchdb-and-haml-erdnodeflips/">first part of this tutorial</a> if you haven&#8217;t already read it.</div>
<p>In the first part of this tutorial we looked at reading a document from CouchDB and keeping our markup separate from our code using Haml templates. We arrived at the point where we clicked on a link, but nothing happened. Today we are going to put that right with some very simple routing.</p>
<h2>The URL Module</h2>
<p>The basis of all routing is the URL. Fortunately, Node has some basic functions which handle <a href="http://nodejs.org/api.html#url-276">reading the URL</a> so we can then decide what the user is actually trying to do.</p>
<p>First, we want to make sure we have included the URL module.</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> sys <span style="color: #339933;">=</span> require<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'sys'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
fs <span style="color: #339933;">=</span> require<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'fs'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
http <span style="color: #339933;">=</span> require<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'http'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
url <span style="color: #339933;">=</span> require<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'url'</span><span style="color: #009900;">&#41;</span>;</pre></div></div>

<p>The next step is to extrapolate the path name from the URL that the user has visited:</p>
<p><span id="more-352"></span></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> url_parts <span style="color: #339933;">=</span> url.<span style="color: #660066;">parse</span><span style="color: #009900;">&#40;</span>req.<span style="color: #660066;">url</span><span style="color: #009900;">&#41;</span>;
sys.<span style="color: #660066;">puts</span><span style="color: #009900;">&#40;</span>url_parts.<span style="color: #660066;">pathname</span><span style="color: #009900;">&#41;</span>;</pre></div></div>

<p>Now, when I go to http://127.0.0.1:8000/create in Chromium, the system output is as follows:<br />
/create<br />
/favicon.ico</p>
<p>As you can see I have to deal with /create but also /favicon.ico &#8211; unfortunately I don&#8217;t have one. We&#8217;ll deal with that later by using a 404 error.</p>
<p>The pages we want to be dealing with are as follows:</p>
<ul>
<li>/</li>
<li>create</li>
<li>/edit</li>
</ul>
<p>For simplicity&#8217;s sake we&#8217;ll put them into a switch statement:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript javascript" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">switch</span><span style="color: #009900;">&#40;</span>url_parts.<span style="color: #660066;">pathname</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
<span style="color: #000066; font-weight: bold;">case</span> <span style="color: #3366CC;">'/'</span><span style="color: #339933;">:</span>
sys.<span style="color: #660066;">puts</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;display root&quot;</span><span style="color: #009900;">&#41;</span>;
<span style="color: #000066; font-weight: bold;">break</span>;
<span style="color: #000066; font-weight: bold;">case</span> <span style="color: #3366CC;">'/create'</span><span style="color: #339933;">:</span>
sys.<span style="color: #660066;">puts</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;display create&quot;</span><span style="color: #009900;">&#41;</span>;
<span style="color: #000066; font-weight: bold;">break</span>;
<span style="color: #000066; font-weight: bold;">case</span> <span style="color: #3366CC;">'/edit'</span><span style="color: #339933;">:</span>
sys.<span style="color: #660066;">puts</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;display edit&quot;</span><span style="color: #009900;">&#41;</span>;
<span style="color: #000066; font-weight: bold;">break</span>;
<span style="color: #003366; font-weight: bold;">default</span><span style="color: #339933;">:</span>
sys.<span style="color: #660066;">puts</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;oh dear, 404&quot;</span><span style="color: #009900;">&#41;</span>;
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>If you test this out you should see the relevant text displayed in the standard output.</p>
<h2 style="font-size: 1.5em;">Tidy up Existing Code</h2>
<p>Now we have a basic router telling us that it has detected which page the user wants to display, but is then displaying the default page regardless. To correct this we need to put our existing code within a function and then call that function in the relevant part of the switch statement.</p>
<p>So, create a new (sub) function called display_root() in the main bulk of the http.createServer() function, expecting a url, a server request object and aserver response object as parameters:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> display_root<span style="color: #009900;">&#40;</span>url<span style="color: #339933;">,</span> req<span style="color: #339933;">,</span> res<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
...
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Within the body of the function, you want to insert the main bulk of the application that we wrote last time. I have included the complete code at the bottom of this tutorial, and all code is of course on <a href="http://github.com/ibrow/erdnodeflips">GitHub</a>.</p>
<p>Now call this function within the switch statement:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript javascript" style="font-family:monospace;">display_root<span style="color: #009900;">&#40;</span>url_parts.<span style="color: #660066;">pathname</span><span style="color: #339933;">,</span> req<span style="color: #339933;">,</span> res<span style="color: #009900;">&#41;</span>;</pre></div></div>

<h2>Handling Missing Pages</h2>
<p>As said, Chromium is looking for a favicon.ico file, but currently this application doesn&#8217;t have one. As such it needs to be able to handle missing files gracefully, with the correct 404 response.</p>
<p>Create a new function within the main bulk of the http.createServer() function:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> display_404<span style="color: #009900;">&#40;</span>url<span style="color: #339933;">,</span> req<span style="color: #339933;">,</span> res<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
res.<span style="color: #660066;">writeHead</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">404</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span><span style="color: #3366CC;">'Content-Type'</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">'text/html'</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;
res.<span style="color: #000066; font-weight: bold;">write</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;&amp;lt;h1&amp;gt;404 Not Found&amp;lt;/h1&amp;gt;&quot;</span><span style="color: #009900;">&#41;</span>;
res.<span style="color: #660066;">end</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;The page you were looking for: &quot;</span><span style="color: #339933;">+</span>url<span style="color: #339933;">+</span><span style="color: #3366CC;">&quot; can not be found&quot;</span><span style="color: #009900;">&#41;</span>;
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Notice that we have specified the header to be 404.</p>
<p>Now you can call this from within the switch:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript javascript" style="font-family:monospace;">display_404<span style="color: #009900;">&#40;</span>url_parts.<span style="color: #660066;">pathname</span><span style="color: #339933;">,</span> req<span style="color: #339933;">,</span> res<span style="color: #009900;">&#41;</span>;</pre></div></div>

<p>You can test it out by going to http://127.0.0.1:8000/qwerty<br />
You should see:</p>
<p><strong>404 Not Found</strong><br />
The page you were looking for: /qwerty can not be found</p>
<p>We could use a Haml template to move the content out of the code, but for the purposes of this tutorial it will suffice.</p>
<h2>The Form</h2>
<p>Now we want to generate a &#8220;Create&#8221; page &#8211; which will be a form allowing us to enter a list name and 1-5 items. (Note: I have changed from 10 items to 5 for no other reason that I am lazy, and don&#8217;t particularly want 10 things on my list, also it keeps the code a bit more clean!)</p>
<p>First, create the Haml template that will be used. We&#8217;ll keep this very simple for now. The key part is the form generation. After some trial and error, this mark-up seemed to work:</p>

<div class="wp_syntax"><div class="code"><pre class="haml" style="font-family:monospace;">%form(action=url method=&quot;post&quot;)
      :each i in [0,1,2,3,4]
        %p %label Item
           %input(type=&quot;text&quot; name=&quot;item[]&quot;)
      %p
        %input(type=&quot;submit&quot; value=&quot;Create List&quot;)</pre></div></div>

<p>You may want to read up on Haml here: <a href="http://haml-lang.com">http://haml-lang.com</a></p>
<p>The function for displaying the page is as follows:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript javascript" style="font-family:monospace;">    <span style="color: #003366; font-weight: bold;">function</span> display_create<span style="color: #009900;">&#40;</span>url<span style="color: #339933;">,</span> req<span style="color: #339933;">,</span> res<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	res.<span style="color: #660066;">writeHead</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">200</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span><span style="color: #3366CC;">'Content-Type'</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">'text/html'</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;
	fs.<span style="color: #660066;">readFile</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'./templates/create.haml'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>e<span style="color: #339933;">,</span> c<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	    <span style="color: #003366; font-weight: bold;">var</span> data <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
		title<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;Create New List&quot;</span><span style="color: #339933;">,</span>
		message<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;Please enter up to 5 things to remember&quot;</span><span style="color: #339933;">,</span>
		url<span style="color: #339933;">:</span> url
	    <span style="color: #009900;">&#125;</span>;
	    <span style="color: #003366; font-weight: bold;">var</span> html <span style="color: #339933;">=</span> haml.<span style="color: #660066;">render</span><span style="color: #009900;">&#40;</span>c.<span style="color: #660066;">toString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span>locals<span style="color: #339933;">:</span> data<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;
	    res.<span style="color: #660066;">end</span><span style="color: #009900;">&#40;</span>html<span style="color: #009900;">&#41;</span>;
	<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;
    <span style="color: #009900;">&#125;</span></pre></div></div>

<p>Now if you fill in the items and submit, the page simply refreshes itelf. We need to get hold of the posted form data and do something useful with it.</p>
<p>This will be what we&#8217;ll focus on next time.</p>
<h2>Tutorial Recap Part 2</h2>
<p>In part 2 of the Node JS tutorial, we started to explore some very basic routing techniques. A number of dedicated routing modules have sprung up, of two of which are listed here:</p>
<ul>
<li>Tim Caswell&#8217;s <a href="http://github.com/creationix/node-router">node-router</a></li>
<li>Tom Hughes-Croucher&#8217;s <a href="http://github.com/sh1mmer/node-routing">node-routing</a></li>
<li>Find more on the <a href="http://wiki.github.com/ry/node/modules">Node Modules wiki page</a></li>
</ul>
<h3>Complete Code</h3>

<div class="wp_syntax"><div class="code"><pre class="javascript javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> sys <span style="color: #339933;">=</span> require<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'sys'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
    fs <span style="color: #339933;">=</span> require<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'fs'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
    http <span style="color: #339933;">=</span> require<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'http'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
    url <span style="color: #339933;">=</span> require<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'url'</span><span style="color: #009900;">&#41;</span>;
&nbsp;
<span style="color: #006600; font-style: italic;">// Node-CouchDB: http://github.com/felixge/node-couchdb</span>
<span style="color: #003366; font-weight: bold;">var</span> couchdb <span style="color: #339933;">=</span> require<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'./libs/node-couchdb/lib/couchdb'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
    client <span style="color: #339933;">=</span> couchdb.<span style="color: #660066;">createClient</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">5984</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'localhost'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
    db <span style="color: #339933;">=</span> client.<span style="color: #660066;">db</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'erdnodeflips'</span><span style="color: #009900;">&#41;</span>;
&nbsp;
<span style="color: #006600; font-style: italic;">// Haml-js: http://github.com/creationix/haml-js</span>
<span style="color: #003366; font-weight: bold;">var</span> haml <span style="color: #339933;">=</span> require<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'./libs/haml-js/lib/haml'</span><span style="color: #009900;">&#41;</span>;
&nbsp;
<span style="color: #003366; font-weight: bold;">var</span> doc_id <span style="color: #339933;">=</span> <span style="color: #3366CC;">'2d36f401bc4b82c9160e1a4ea936aba3'</span>;
&nbsp;
http.<span style="color: #660066;">createServer</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span>req<span style="color: #339933;">,</span> res<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #003366; font-weight: bold;">var</span> url_parts <span style="color: #339933;">=</span> url.<span style="color: #660066;">parse</span><span style="color: #009900;">&#40;</span>req.<span style="color: #660066;">url</span><span style="color: #009900;">&#41;</span>;
&nbsp;
    <span style="color: #000066; font-weight: bold;">switch</span><span style="color: #009900;">&#40;</span>url_parts.<span style="color: #660066;">pathname</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">case</span> <span style="color: #3366CC;">'/'</span><span style="color: #339933;">:</span>
	display_root<span style="color: #009900;">&#40;</span>url_parts.<span style="color: #660066;">pathname</span><span style="color: #339933;">,</span> req<span style="color: #339933;">,</span> res<span style="color: #009900;">&#41;</span>;
	<span style="color: #000066; font-weight: bold;">break</span>;
    <span style="color: #000066; font-weight: bold;">case</span> <span style="color: #3366CC;">'/create'</span><span style="color: #339933;">:</span>
	display_create<span style="color: #009900;">&#40;</span>url_parts.<span style="color: #660066;">pathname</span><span style="color: #339933;">,</span> req<span style="color: #339933;">,</span> res<span style="color: #009900;">&#41;</span>;
	<span style="color: #000066; font-weight: bold;">break</span>;
    <span style="color: #000066; font-weight: bold;">case</span> <span style="color: #3366CC;">'/edit'</span><span style="color: #339933;">:</span>
	sys.<span style="color: #660066;">puts</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;display edit&quot;</span><span style="color: #009900;">&#41;</span>;
	<span style="color: #000066; font-weight: bold;">break</span>;
    <span style="color: #003366; font-weight: bold;">default</span><span style="color: #339933;">:</span>
	display_404<span style="color: #009900;">&#40;</span>url_parts.<span style="color: #660066;">pathname</span><span style="color: #339933;">,</span> req<span style="color: #339933;">,</span> res<span style="color: #009900;">&#41;</span>;
    <span style="color: #009900;">&#125;</span>
    <span style="color: #000066; font-weight: bold;">return</span>;
&nbsp;
    <span style="color: #006600; font-style: italic;">/**
     * Display the document root
     **/</span>
    <span style="color: #003366; font-weight: bold;">function</span> display_root<span style="color: #009900;">&#40;</span>url<span style="color: #339933;">,</span> req<span style="color: #339933;">,</span> res<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	res.<span style="color: #660066;">writeHead</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">200</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span><span style="color: #3366CC;">'Content-Type'</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">'text/html'</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;
	db.<span style="color: #660066;">getDoc</span><span style="color: #009900;">&#40;</span>doc_id<span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>error<span style="color: #339933;">,</span> doc<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>error<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		fs.<span style="color: #660066;">readFile</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'./templates/no-doc.haml'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>e<span style="color: #339933;">,</span> c<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		    <span style="color: #003366; font-weight: bold;">var</span> data <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
			title<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;No Document Found&quot;</span><span style="color: #339933;">,</span>
			message<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;No document could be found&quot;</span><span style="color: #339933;">,</span>
			link<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;/create&quot;</span><span style="color: #339933;">,</span>
			link_text<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;Create a new document&quot;</span>
		    <span style="color: #009900;">&#125;</span>;
		    <span style="color: #003366; font-weight: bold;">var</span> html <span style="color: #339933;">=</span> haml.<span style="color: #660066;">render</span><span style="color: #009900;">&#40;</span>c.<span style="color: #660066;">toString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span>locals<span style="color: #339933;">:</span> data<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;
		    res.<span style="color: #660066;">end</span><span style="color: #009900;">&#40;</span>html<span style="color: #009900;">&#41;</span>;
		<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;
	    <span style="color: #009900;">&#125;</span>
	    <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
		fs.<span style="color: #660066;">readFile</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'./templates/doc.haml'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>e<span style="color: #339933;">,</span> c<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		    <span style="color: #003366; font-weight: bold;">var</span> data <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
			title<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;Erdnodeflip document: &quot;</span><span style="color: #339933;">+</span>doc.<span style="color: #000066;">name</span><span style="color: #339933;">,</span>
			message<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;Your Erdnusflip document was found!&quot;</span><span style="color: #339933;">,</span>
			items<span style="color: #339933;">:</span> doc.<span style="color: #660066;">items</span><span style="color: #339933;">,</span>
		    <span style="color: #009900;">&#125;</span>;
		    <span style="color: #003366; font-weight: bold;">var</span> html <span style="color: #339933;">=</span> haml.<span style="color: #660066;">render</span><span style="color: #009900;">&#40;</span>c.<span style="color: #660066;">toString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span>locals<span style="color: #339933;">:</span> data<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;
		    res.<span style="color: #660066;">end</span><span style="color: #009900;">&#40;</span>html<span style="color: #009900;">&#41;</span>;
		<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;
	    <span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #006600; font-style: italic;">/**
     * Display the list creat page
     **/</span>
    <span style="color: #003366; font-weight: bold;">function</span> display_create<span style="color: #009900;">&#40;</span>url<span style="color: #339933;">,</span> req<span style="color: #339933;">,</span> res<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	res.<span style="color: #660066;">writeHead</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">200</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span><span style="color: #3366CC;">'Content-Type'</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">'text/html'</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;
	fs.<span style="color: #660066;">readFile</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'./templates/create.haml'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>e<span style="color: #339933;">,</span> c<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	    <span style="color: #003366; font-weight: bold;">var</span> data <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
		title<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;Create New List&quot;</span><span style="color: #339933;">,</span>
		message<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;Please enter up to 5 things to remember&quot;</span><span style="color: #339933;">,</span>
		url<span style="color: #339933;">:</span> url
	    <span style="color: #009900;">&#125;</span>;
	    <span style="color: #003366; font-weight: bold;">var</span> html <span style="color: #339933;">=</span> haml.<span style="color: #660066;">render</span><span style="color: #009900;">&#40;</span>c.<span style="color: #660066;">toString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span>locals<span style="color: #339933;">:</span> data<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;
	    res.<span style="color: #660066;">end</span><span style="color: #009900;">&#40;</span>html<span style="color: #009900;">&#41;</span>;
	<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #006600; font-style: italic;">/**
     * Display the 404 page for content that can't be found
     **/</span>
    <span style="color: #003366; font-weight: bold;">function</span> display_404<span style="color: #009900;">&#40;</span>url<span style="color: #339933;">,</span> req<span style="color: #339933;">,</span> res<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	res.<span style="color: #660066;">writeHead</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">404</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span><span style="color: #3366CC;">'Content-Type'</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">'text/html'</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;
	res.<span style="color: #000066; font-weight: bold;">write</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;
&lt;h1&gt;404 Not Found&lt;/h1&gt;
&quot;</span><span style="color: #009900;">&#41;</span>;
	res.<span style="color: #660066;">end</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;
&nbsp;
The page you were looking for: &quot;</span><span style="color: #339933;">+</span>url<span style="color: #339933;">+</span><span style="color: #3366CC;">&quot; can not be found&quot;</span><span style="color: #009900;">&#41;</span>;
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">listen</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">8000</span><span style="color: #009900;">&#41;</span>;
sys.<span style="color: #660066;">puts</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'Server running at http://127.0.0.1:8000/'</span><span style="color: #009900;">&#41;</span>;</pre></div></div>

<img src="http://www.robsearles.com/?ak_action=api_record_view&id=352&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://www.robsearles.com/2010/05/31/nodejs-tutorial-part-2-routing/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>NodeJS Tutorial with CouchDB and Haml &#8211; ErdNodeFlips</title>
		<link>http://www.robsearles.com/2010/05/28/nodejs-tutorial-with-couchdb-and-haml-erdnodeflips/</link>
		<comments>http://www.robsearles.com/2010/05/28/nodejs-tutorial-with-couchdb-and-haml-erdnodeflips/#comments</comments>
		<pubDate>Fri, 28 May 2010 10:38:37 +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=338</guid>
		<description><![CDATA[A tutorial for creating a basic Node JS application. Using Node, CouchDB and Haml]]></description>
			<content:encoded><![CDATA[<p>The aim of this tutorial is to create a <a href="http://nodejs.org">Node</a> powered web app using a <a href="http://couchdb.apache.org/">CouchDB</a> database and <a href="http://haml-lang.com/">Haml</a> based templates. I wanted something relatively simple that I could do in a couple of sittings, but complex enough to be of some actual use. In the end I opted for a &#8220;top 10 list&#8221; type application. The functionality being as follows:</p>
<ol>
<li>To list a number of items</li>
<li>To remove an item from the list</li>
<li>To add an item to the list</li>
</ol>
<p>Obviously, before getting cracking with this tutorial I had to think up a name for this &#8220;killer app&#8221; of the Node world.</p>
<p>So I give you Erd<strong>Node</strong>Flips! In honour of <a href="http://de.wikipedia.org/wiki/Erdnussflips">erdnussflips</a> &#8211; the strangely addictive <a href="http://en.wikipedia.org/wiki/Wotsits">Wotsit</a> type things that taste of peanut butter and stick to the top of your mouth. Yum! (OMG &#8211; they even have their own <a href="http://www.facebook.com/pages/Erdnussflips/110701296397">Facebook page</a>!)</p>
<p>All code is available on <a href="http://github.com/ibrow/erdnodeflips">GitHub</a>.</p>
<p><span id="more-338"></span></p>
<h2>Directory Structure and Libraries</h2>
<p>The first step is to create a directory structure for the application and bring in the relevant libraries. We need two directories underneath the top-level application root: templates (which will hold the haml files) and libs (which will contain 3rd party libraries). We are going to grab two libraries off <a href="http://gitbub.com">GitHub</a>:</p>
<ul>
<li><a href="http://github.com/felixge/node-couchdb">Felix Geisendörfer&#8217;s node-couchdb</a> &#8211; this is what we&#8217;ll be using to interact with the Couch database</li>
<li><a href="http://github.com/creationix/haml-js">Tim Caswell&#8217;s haml-js</a> &#8211; this is our haml template engine</li>
</ul>

<div class="wp_syntax"><div class="code"><pre class="bash bash" style="font-family:monospace;">$ <span style="color: #7a0874; font-weight: bold;">cd</span> <span style="color: #000000; font-weight: bold;">/</span>
$ <span style="color: #c20cb9; font-weight: bold;">mkdir</span> <span style="color: #7a0874; font-weight: bold;">&#123;</span>libs,templates<span style="color: #7a0874; font-weight: bold;">&#125;</span>
$ <span style="color: #7a0874; font-weight: bold;">cd</span> libs
$ git clone git:<span style="color: #000000; font-weight: bold;">//</span>github.com<span style="color: #000000; font-weight: bold;">/</span>felixge<span style="color: #000000; font-weight: bold;">/</span>node-couchdb.git
$ git clone http:<span style="color: #000000; font-weight: bold;">//</span>github.com<span style="color: #000000; font-weight: bold;">/</span>creationix<span style="color: #000000; font-weight: bold;">/</span>haml-js.git
$ <span style="color: #7a0874; font-weight: bold;">cd</span> ..<span style="color: #000000; font-weight: bold;">/</span></pre></div></div>

<h2>Basic Web Server</h2>
<p>Now we can get started. Open up a new file called <strong>main.js</strong> in your <a href="http://www.robsearles.com/2009/10/25/a-week-with-emacs-one-week-later/">favourite editor</a>. Now, just to make sure that everything is working at this early stage you can copy and paste the http server example from the nodejs.org front page and run that.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript javascript" style="font-family:monospace;">http.<span style="color: #660066;">createServer</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span>req<span style="color: #339933;">,</span> res<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</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>
    res.<span style="color: #660066;">writeHead</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">200</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span><span style="color: #3366CC;">'Content-Type'</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">'text/plain'</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;
    res.<span style="color: #660066;">end</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'Hello World<span style="color: #000099; font-weight: bold;">\n</span>'</span><span style="color: #009900;">&#41;</span>;
  <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">2000</span><span style="color: #009900;">&#41;</span>;
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">listen</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">8000</span><span style="color: #009900;">&#41;</span>;</pre></div></div>


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

<p>Navigate to http://127.0.0.1:8000/ and after 2 seconds you should see &#8220;Hello World&#8221; appear. If you see this, all is working. We have the (very basic) base of our application.</p>
<h2>Talk to the Database</h2>
<p>Once we know that our basic setup is working, we can try and interact with the database. Before we can do this, however, we actually need to create the database and add an record &#8211; or document.</p>
<p>CouchDB comes with a nifty little utility called <a href="http://couchdb.apache.org/screenshots.html">Futon</a>. We&#8217;ll use this to quickly create our database and add a document.</p>
<ul>
<li>Go to http://localhost:5984/_utils/</li>
<li>Create a new database: <strong>erdnodeflips</strong></li>
<li>Create a new document</li>
<li>Add Field &#8211; name it <strong>name</strong></li>
<li>Double click on the value for <strong>name</strong>, enter<strong> &#8220;Don&#8217;t forget&#8221;</strong> (including the quotes)</li>
<li>Add another field &#8211; name it <strong>items</strong></li>
<li>Double click on the value for <strong>items</strong> enter: <strong>["item 1", "item 2", "item 3"]</strong> (including quotes and square brackets)</li>
<li>Click on Save Document &#8211; copy the <strong>_id</strong> value</li>
<li>Navigate to: http://localhost:5984/erdnodeflips/[value of "_id"] to see something like: {&#8221;_id&#8221;:&#8221;2d36f401bc4b82c9160e1a4ea936aba3&#8243;,&#8221;_rev&#8221;:&#8221;2-3b9f738a50ff9928348ce32b0eb807b2&#8243;,&#8221;items&#8221;:["item 1","item 2","item 3"],&#8221;name&#8221;:&#8221;Rob&#8217;s List&#8221;}</li>
</ul>
<p>To read this within node we simply need to know the _id of the document. Save this as a var for now:</p>
<p>var doc_id = &#8216;2d36f401bc4b82c9160e1a4ea936aba3&#8242;;</p>
<p>and replace the setTimeout function with the following:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript javascript" style="font-family:monospace;">db.<span style="color: #660066;">getDoc</span><span style="color: #009900;">&#40;</span>doc_id<span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>error<span style="color: #339933;">,</span> doc<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>error<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	    res.<span style="color: #000066; font-weight: bold;">write</span><span style="color: #009900;">&#40;</span>JSON.<span style="color: #660066;">stringify</span><span style="color: #009900;">&#40;</span>error<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>;
	<span style="color: #009900;">&#125;</span>
	<span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
	    res.<span style="color: #000066; font-weight: bold;">write</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'Fetched my new doc from couch:'</span><span style="color: #009900;">&#41;</span>;
	    res.<span style="color: #000066; font-weight: bold;">write</span><span style="color: #009900;">&#40;</span>JSON.<span style="color: #660066;">stringify</span><span style="color: #009900;">&#40;</span>doc<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>;
	<span style="color: #009900;">&#125;</span>
     res.<span style="color: #660066;">end</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;-= Fin =-&quot;</span><span style="color: #009900;">&#41;</span>;
    <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;</pre></div></div>

<p>This is pretty simple, and should be relatively straight forward to read. Essentially the code tries to get the document identified by doc_id, invoking a function when done: <strong>db.getDoc(doc_id, function(error, doc){});</strong></p>
<p>The function that is invoked has 2 arguments, one containing the error (if any) and the second containing the actual document returned (if any).</p>
<p>Within this function we can then deal with the two arguments as needed. If there is an error, we output that to the browser, if no error and a doc is returned, output that instead.</p>
<p>Finally, we end the server response: <strong>res.end(&#8221;-= Fin =-&#8221;);</strong>. Note, we must end the server response within the callback of the getDoc() function. Due to the event driven, asynchronous nature of Node, if we end the response after that function then the server response will more than likely be closed before getDoc() has had a chance to write the additional server responses.</p>
<p>So the code now looks like this:</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> sys <span style="color: #339933;">=</span> require<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'sys'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
    fs <span style="color: #339933;">=</span> require<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'fs'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
    http <span style="color: #339933;">=</span> require<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'http'</span><span style="color: #009900;">&#41;</span>;
&nbsp;
<span style="color: #006600; font-style: italic;">// Node-CouchDB: http://github.com/felixge/node-couchdb</span>
<span style="color: #003366; font-weight: bold;">var</span> couchdb <span style="color: #339933;">=</span> require<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'./libs/node-couchdb/lib/couchdb'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
    client <span style="color: #339933;">=</span> couchdb.<span style="color: #660066;">createClient</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">5984</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'localhost'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
    db <span style="color: #339933;">=</span> client.<span style="color: #660066;">db</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'erdnodeflips'</span><span style="color: #009900;">&#41;</span>;
&nbsp;
<span style="color: #006600; font-style: italic;">// Haml-js: http://github.com/creationix/haml-js</span>
<span style="color: #003366; font-weight: bold;">var</span> haml <span style="color: #339933;">=</span> require<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'./libs/haml-js/lib/haml'</span><span style="color: #009900;">&#41;</span>;
&nbsp;
<span style="color: #003366; font-weight: bold;">var</span> doc_id <span style="color: #339933;">=</span> <span style="color: #3366CC;">'2d36f401bc4b82c9160e1a4ea936aba3'</span>;
&nbsp;
http.<span style="color: #660066;">createServer</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span>req<span style="color: #339933;">,</span> res<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    res.<span style="color: #660066;">writeHead</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">200</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span><span style="color: #3366CC;">'Content-Type'</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">'text/html'</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;
    db.<span style="color: #660066;">getDoc</span><span style="color: #009900;">&#40;</span>doc_id<span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>error<span style="color: #339933;">,</span> doc<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>error<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	    res.<span style="color: #000066; font-weight: bold;">write</span><span style="color: #009900;">&#40;</span>JSON.<span style="color: #660066;">stringify</span><span style="color: #009900;">&#40;</span>error<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>;
	<span style="color: #009900;">&#125;</span>
	<span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
	    res.<span style="color: #000066; font-weight: bold;">write</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'Fetched my new doc from couch:'</span><span style="color: #009900;">&#41;</span>;
	    res.<span style="color: #000066; font-weight: bold;">write</span><span style="color: #009900;">&#40;</span>JSON.<span style="color: #660066;">stringify</span><span style="color: #009900;">&#40;</span>doc<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>;
	<span style="color: #009900;">&#125;</span>
     res.<span style="color: #660066;">end</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;-= Fin =-&quot;</span><span style="color: #009900;">&#41;</span>;
    <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;
&nbsp;
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">listen</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">8000</span><span style="color: #009900;">&#41;</span>;
sys.<span style="color: #660066;">puts</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'Server running at http://127.0.0.1:8000/'</span><span style="color: #009900;">&#41;</span>;</pre></div></div>

<h2>Generate Nicer Output</h2>
<p>If you run the above code, you should get something along the lines of:</p>
<p>Fetched my new doc from couch:{&#8221;_id&#8221;:&#8221;2d36f401bc4b82c9160e1a4ea936aba3&#8243;,&#8221;_rev&#8221;:&#8221;2-3b9f738a50ff9928348ce32b0eb807b2&#8243;,&#8221;items&#8221;:["item 1","item 2","item 3"],&#8221;name&#8221;:&#8221;Rob&#8217;s List&#8221;}-= Fin =-</p>
<p>So it works, but it doesn&#8217;t look very good.</p>
<p>First, lets create a Haml template that we can use to output something better.</p>

<div class="wp_syntax"><div class="code"><pre class="haml" style="font-family:monospace;">!!! Strict
%html(lang=&quot;en&quot;)
  %head
    %title&amp;amp;= title
  %body
    %h1&amp;amp;= title
    %p&amp;amp;= message
    :if items.length === 0
      %p There are no items to remember
    :if items.length &amp;gt; 0
      %ul
        :each item in items
          %li&amp;amp;=</pre></div></div>

<p>I&#8217;m not going to go into great depth. You can find more information on Haml at the <a href="http://haml-lang.com/">Haml homepage</a> and also have a look at Tim&#8217;s <a href="http://howtonode.org/haml-for-javascript">excellent tutorial</a> on his Haml-js library.</p>
<p>Essentially, this template is going to build an XHTML strict page that will have a title, a message and then should loop through the items in the list. To get this displayed, we then need to open up the template file, render it and then output it as a server response. The code to do that is:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript javascript" style="font-family:monospace;">fs.<span style="color: #660066;">readFile</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'./templates/doc.haml'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>e<span style="color: #339933;">,</span> c<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #003366; font-weight: bold;">var</span> data <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
		    title<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;Erdnodeflip document: &quot;</span><span style="color: #339933;">+</span>doc.<span style="color: #000066;">name</span><span style="color: #339933;">,</span>
		    message<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;Your Erdnusflip document was found!&quot;</span><span style="color: #339933;">,</span>
		    items<span style="color: #339933;">:</span> doc.<span style="color: #660066;">items</span><span style="color: #339933;">,</span>
		<span style="color: #009900;">&#125;</span>;
		<span style="color: #003366; font-weight: bold;">var</span> html <span style="color: #339933;">=</span> haml.<span style="color: #660066;">render</span><span style="color: #009900;">&#40;</span>c.<span style="color: #660066;">toString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span>locals<span style="color: #339933;">:</span> data<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;
		res.<span style="color: #660066;">end</span><span style="color: #009900;">&#40;</span>html<span style="color: #009900;">&#41;</span>;
	    <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;</pre></div></div>

<p>Notice that I am simply ending the response with the generated HTML. Again, it is worth keeping an eye on exactly where the response is being ended otherwise you might find you are trying to add to a conversation that has already ended.</p>
<p>We can do the same for the &#8220;document not found&#8221; route:</p>

<div class="wp_syntax"><div class="code"><pre class="haml" style="font-family:monospace;">!!! Strict
%html(lang=&quot;en&quot;)
  %head
    %title&amp;amp;= title
  %body
    %h1&amp;amp;= title
    %p&amp;amp;= message
    %p %a{href: link}&amp;amp;= link_text</pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="javascript javascript" style="font-family:monospace;">fs.<span style="color: #660066;">readFile</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'./templates/no-doc.haml'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>e<span style="color: #339933;">,</span> c<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #003366; font-weight: bold;">var</span> data <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
		    title<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;No Document Found&quot;</span><span style="color: #339933;">,</span>
		    message<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;No document could be found&quot;</span><span style="color: #339933;">,</span>
		    link<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;/create&quot;</span><span style="color: #339933;">,</span>
		    link_text<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;Create a new document&quot;</span>
		<span style="color: #009900;">&#125;</span>;
		<span style="color: #003366; font-weight: bold;">var</span> html <span style="color: #339933;">=</span> haml.<span style="color: #660066;">render</span><span style="color: #009900;">&#40;</span>c.<span style="color: #660066;">toString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span>locals<span style="color: #339933;">:</span> data<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;
		res.<span style="color: #660066;">end</span><span style="color: #009900;">&#40;</span>html<span style="color: #009900;">&#41;</span>;</pre></div></div>

<h2>Quick Recap for this Tutorial</h2>
<p>In this tutorial we&#8217;ve only scratched the surface of what can be done. However, in a relatively short space of time we have our Node web server communicating with a CouchDB database and displaying some valid strict XHTML. Not bad for one sitting!</p>
<p>To recap, our code now looks like this:</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> sys <span style="color: #339933;">=</span> require<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'sys'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
    fs <span style="color: #339933;">=</span> require<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'fs'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
    http <span style="color: #339933;">=</span> require<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'http'</span><span style="color: #009900;">&#41;</span>;
&nbsp;
<span style="color: #006600; font-style: italic;">// Node-CouchDB: http://github.com/felixge/node-couchdb</span>
<span style="color: #003366; font-weight: bold;">var</span> couchdb <span style="color: #339933;">=</span> require<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'./libs/node-couchdb/lib/couchdb'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
    client <span style="color: #339933;">=</span> couchdb.<span style="color: #660066;">createClient</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">5984</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'localhost'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
    db <span style="color: #339933;">=</span> client.<span style="color: #660066;">db</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'erdnodeflips'</span><span style="color: #009900;">&#41;</span>;
&nbsp;
<span style="color: #006600; font-style: italic;">// Haml-js: http://github.com/creationix/haml-js</span>
<span style="color: #003366; font-weight: bold;">var</span> haml <span style="color: #339933;">=</span> require<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'./libs/haml-js/lib/haml'</span><span style="color: #009900;">&#41;</span>;
&nbsp;
<span style="color: #003366; font-weight: bold;">var</span> doc_id <span style="color: #339933;">=</span> <span style="color: #3366CC;">'2d36f401bc4b82c9160e1a4ea936aba3'</span>;
&nbsp;
http.<span style="color: #660066;">createServer</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span>req<span style="color: #339933;">,</span> res<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    res.<span style="color: #660066;">writeHead</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">200</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span><span style="color: #3366CC;">'Content-Type'</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">'text/html'</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;
    db.<span style="color: #660066;">getDoc</span><span style="color: #009900;">&#40;</span>doc_id<span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>error<span style="color: #339933;">,</span> doc<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>error<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	    fs.<span style="color: #660066;">readFile</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'./templates/no-doc.haml'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>e<span style="color: #339933;">,</span> c<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #003366; font-weight: bold;">var</span> data <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
		    title<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;No Document Found&quot;</span><span style="color: #339933;">,</span>
		    message<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;No document could be found&quot;</span><span style="color: #339933;">,</span>
		    link<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;/create&quot;</span><span style="color: #339933;">,</span>
		    link_text<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;Create a new document&quot;</span>
		<span style="color: #009900;">&#125;</span>;
		<span style="color: #003366; font-weight: bold;">var</span> html <span style="color: #339933;">=</span> haml.<span style="color: #660066;">render</span><span style="color: #009900;">&#40;</span>c.<span style="color: #660066;">toString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span>locals<span style="color: #339933;">:</span> data<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;
		res.<span style="color: #660066;">end</span><span style="color: #009900;">&#40;</span>html<span style="color: #009900;">&#41;</span>;
	    <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;
	<span style="color: #009900;">&#125;</span>
	<span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
	    fs.<span style="color: #660066;">readFile</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'./templates/doc.haml'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>e<span style="color: #339933;">,</span> c<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #003366; font-weight: bold;">var</span> data <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
		    title<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;Erdnodeflip document: &quot;</span><span style="color: #339933;">+</span>doc.<span style="color: #000066;">name</span><span style="color: #339933;">,</span>
		    message<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;Your Erdnusflip document was found!&quot;</span><span style="color: #339933;">,</span>
		    items<span style="color: #339933;">:</span> doc.<span style="color: #660066;">items</span><span style="color: #339933;">,</span>
		<span style="color: #009900;">&#125;</span>;
		<span style="color: #003366; font-weight: bold;">var</span> html <span style="color: #339933;">=</span> haml.<span style="color: #660066;">render</span><span style="color: #009900;">&#40;</span>c.<span style="color: #660066;">toString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span>locals<span style="color: #339933;">:</span> data<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;
		res.<span style="color: #660066;">end</span><span style="color: #009900;">&#40;</span>html<span style="color: #009900;">&#41;</span>;
	    <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;
	<span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;
&nbsp;
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">listen</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">8000</span><span style="color: #009900;">&#41;</span>;
sys.<span style="color: #660066;">puts</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'Server running at http://127.0.0.1:8000/'</span><span style="color: #009900;">&#41;</span>;</pre></div></div>

<p>The exciting thing here is that we&#8217;ve thrown in a link! However, if you click on it, it will simply reload the page.</p>
<p>Being able to handle this kind of basic page navigation and actually adding stuff to the database is for next time.</p>
<p>Feedback always welcome. You can find all the code on my <a href="http://github.com/ibrow/erdnodeflips">GitHub pages</a>.</p>
<p><strong>Valuable Node resources:</strong></p>
<ul>
<li>The <a href="http://nodejs.org">official Node JS</a> site</li>
<li>The <a href="http://github.com/ry/node/">Node Github</a> page</li>
<li><a href="http://howtonode.org/">How To Node</a> &#8211; a great place for tutorials and other tips</li>
<li><a href="http://nodeblogs.com/">Node Blogs</a> &#8211; which appears to be down at the moment, but again is very useful</li>
<li><a href="http://groups.google.com/group/nodejs">NodeJS Google Group</a> &#8211; keep your finger on the pulse</li>
</ul>
<img src="http://www.robsearles.com/?ak_action=api_record_view&id=338&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://www.robsearles.com/2010/05/28/nodejs-tutorial-with-couchdb-and-haml-erdnodeflips/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
		<item>
		<title>jQuery Validate URL, adding &#8220;http://&#8221;</title>
		<link>http://www.robsearles.com/2010/05/27/jquery-validate-url-adding-http/</link>
		<comments>http://www.robsearles.com/2010/05/27/jquery-validate-url-adding-http/#comments</comments>
		<pubDate>Thu, 27 May 2010 11:36:27 +0000</pubDate>
		<dc:creator>Rob Searles</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[jQuery]]></category>

		<guid isPermaLink="false">http://www.robsearles.com/?p=333</guid>
		<description><![CDATA[A project we have going on here at ibrow towers involves quite a bit of jQuery Validation. Whilst the Validation plugin is great with the default functions it offers, what makes it truly excellent is the fact that it is easily extendable.
For example, one thing that has been bugging me is to validate a URL [...]]]></description>
			<content:encoded><![CDATA[<p>A <a href="http://www.liveunsigned.com">project</a> we have going on here at <a href="http://www.ibrow.com">ibrow towers</a> involves quite a bit of <a href="http://docs.jquery.com/Plugins/Validation/Validator">jQuery Validation</a>. Whilst the Validation plugin is great with the default functions it offers, what makes it truly excellent is the fact that it is easily extendable.</p>
<p>For example, one thing that has been bugging me is to validate a URL the user must type in the initial http://. But why? Can the validation not just assume that if, for example, <strong>www.robsearles.com</strong> is typed in, that the user actually meant <strong>http://www.robsearles.com</strong>?</p>
<p>Well, after a quick read of the documentation and a hack around this little irritant is now gone. It&#8217;s all down to <a href="http://docs.jquery.com/Plugins/Validation/Validator/addMethod">addMethod()</a> which lets you add a custom validation method. Lets create one called <strong>complete_url()</strong>.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript javascript" style="font-family:monospace;">jQuery.<span style="color: #660066;">validator</span>.<span style="color: #660066;">addMethod</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;complete_url&quot;</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>val<span style="color: #339933;">,</span> elem<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #006600; font-style: italic;">// will contain our validation code</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'You must enter a valid URL'</span><span style="color: #009900;">&#41;</span>;</pre></div></div>

<p>A quick word about return values. If the method returns true, everything is considered OK. However, if false is returned then validation is assumed to have failed.</p>
<p>Our new method needs to do three things:</p>
<ol>
<li> If no url, don&#8217;t do anything</li>
<li>Check that the user had entered http:// in their URL, if not, add it in for them.</li>
<li> Check that the URL is now valid. For this I just used the <a href="http://github.com/jzaefferer/jquery-validation/blob/master/jquery.validate.js">source</a> for the original url validation method.</li>
</ol>
<p>Lets translate this into some code:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript javascript" style="font-family:monospace;">jQuery.<span style="color: #660066;">validator</span>.<span style="color: #660066;">addMethod</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;complete_url&quot;</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>val<span style="color: #339933;">,</span> elem<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #006600; font-style: italic;">// if no url, don't do anything</span>
    <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>val.<span style="color: #660066;">length</span> <span style="color: #339933;">==</span> 0<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">true</span>; <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #006600; font-style: italic;">// if user has not entered http:// https:// or ftp:// assume they mean http://</span>
    <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!/^</span><span style="color: #009900;">&#40;</span>https?|ftp<span style="color: #009900;">&#41;</span><span style="color: #339933;">:</span>\<span style="color: #339933;">/</span>\<span style="color: #006600; font-style: italic;">//i.test(val)) {</span>
        val <span style="color: #339933;">=</span> <span style="color: #3366CC;">'http://'</span><span style="color: #339933;">+</span>val; <span style="color: #006600; font-style: italic;">// set both the value</span>
        $<span style="color: #009900;">&#40;</span>elem<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">val</span><span style="color: #009900;">&#40;</span>val<span style="color: #009900;">&#41;</span>; <span style="color: #006600; font-style: italic;">// also update the form element</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #006600; font-style: italic;">// now check if valid url</span>
    <span style="color: #006600; font-style: italic;">// http://docs.jquery.com/Plugins/Validation/Methods/url</span>
    <span style="color: #006600; font-style: italic;">// contributed by Scott Gonzalez: http://projects.scottsplayground.com/iri/</span>
    <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #009966; font-style: italic;">/^(https?|ftp):\/\/(((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&amp;amp;'\(\)\*\+,;=]|:)*@)?(((\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5]))|((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?)(:\d*)?)(\/((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&amp;amp;'\(\)\*\+,;=]|:|@)+(\/(([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&amp;amp;'\(\)\*\+,;=]|:|@)*)*)?)?(\?((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&amp;amp;'\(\)\*\+,;=]|:|@)|[\uE000-\uF8FF]|\/|\?)*)?(\#((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&amp;amp;'\(\)\*\+,;=]|:|@)|\/|\?)*)?$/i</span>.<span style="color: #660066;">test</span><span style="color: #009900;">&#40;</span>val<span style="color: #009900;">&#41;</span>;
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;</pre></div></div>

<p>Validating the form with this new method is very straight forward:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#form1&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">validate</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
    rules<span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span>
        url<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;complete_url&quot;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;</pre></div></div>

<p>Feel free to use/copy/modify as you see fit. If you do use it, why not let me know, or drop me a trackback?</p>
<p>Have fun</p>
<p><strong>Other jQuery Specific Posts</strong></p>
<ul>
<li><a href="http://www.robsearles.com/2010/03/11/ie-7-indexof-replacement/">IE 7 indexOf() Replacement</a></li>
<li><a href="http://www.robsearles.com/2009/01/27/fixed-jquery-datepicker-not-working-in-thickbox/">Fixed – jQuery DatePicker not working in Thickbox</a></li>
<li><a href="http://www.robsearles.com/2008/11/26/jquery-link-override-with-search-box/">jQuery: Link override with search box</a></li>
</ul>
<img src="http://www.robsearles.com/?ak_action=api_record_view&id=333&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://www.robsearles.com/2010/05/27/jquery-validate-url-adding-http/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Easily Add Current File to SVN Within Emacs</title>
		<link>http://www.robsearles.com/2010/05/14/easily-add-current-file-to-svn-within-emacs/</link>
		<comments>http://www.robsearles.com/2010/05/14/easily-add-current-file-to-svn-within-emacs/#comments</comments>
		<pubDate>Fri, 14 May 2010 13:59:59 +0000</pubDate>
		<dc:creator>Rob Searles</dc:creator>
				<category><![CDATA[Emacs]]></category>
		<category><![CDATA[Tutorials]]></category>

		<guid isPermaLink="false">http://www.robsearles.com/?p=310</guid>
		<description><![CDATA[A quick howto for adding the current working file to an SVN repository within Emacs]]></description>
			<content:encoded><![CDATA[<p>As I&#8217;ve now been <a href="http://www.robsearles.com/2009/10/19/a-week-with-emacs/">using Emacs</a> for quite a long time, I have picked up a few tips along the way. One thing I&#8217;ve been doing more and more is using the shell within Emacs. (For more details see <a href="http://jamesthornton.com/emacs/node/emacs_442.html">here</a> and <a href="http://www.gnu.org/software/emacs/manual/html_mono/eshell.html">here</a>.)</p>
<p>Some of my most used shell commands are using SVN, such as add, revert etc. However, I couldn&#8217;t find anywhere to just add/revert the current file I am working on. Fortunately there is <a href="http://stackoverflow.com">Stack Overflow</a>! Someone with a <a href="http://stackoverflow.com/questions/455345/in-emacs-how-to-insert-file-name-in-shell-command">very similar issue</a> to me has had a couple of good answers, which I have &#8220;borrowed&#8221; and now adding files to my SVN repository is really easy.</p>
<p>First, add the following to your <strong>.emacs</strong> file</p>

<div class="wp_syntax"><div class="code"><pre class="lisp lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span>define<span style="color: #66cc66;">-</span>key minibuffer<span style="color: #66cc66;">-</span>local<span style="color: #66cc66;">-</span>map <span style="color: #66cc66;">&#91;</span>f3<span style="color: #66cc66;">&#93;</span>
<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">lambda</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>interactive<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>insert <span style="color: #66cc66;">&#40;</span>buffer<span style="color: #66cc66;">-</span>file<span style="color: #66cc66;">-</span><span style="color: #b1b100;">name</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">nth</span> <span style="color: #cc66cc;">1</span> <span style="color: #66cc66;">&#40;</span>buffer<span style="color: #66cc66;">-</span><span style="color: #b1b100;">list</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>Now either open up Emacs, or reload your <strong>.emacs</strong> file:</p>

<div class="wp_syntax"><div class="code"><pre class="lisp lisp" style="font-family:monospace;">M<span style="color: #66cc66;">-</span>x load<span style="color: #66cc66;">-</span>file RET ~<span style="color: #66cc66;">/</span>.emacs RET</pre></div></div>

<p>Now, when you are working on a new file you can easily add it to SVN with the following command:</p>

<div class="wp_syntax"><div class="code"><pre class="lisp lisp" style="font-family:monospace;">M<span style="color: #66cc66;">-!</span> svn add F3 RET</pre></div></div>

<p>Nice!</p>
<img src="http://www.robsearles.com/?ak_action=api_record_view&id=310&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://www.robsearles.com/2010/05/14/easily-add-current-file-to-svn-within-emacs/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>My Perfect Desktop Day 1 &#8211; The Base System</title>
		<link>http://www.robsearles.com/2010/03/18/my-perfect-desktop-day-1-the-base-system/</link>
		<comments>http://www.robsearles.com/2010/03/18/my-perfect-desktop-day-1-the-base-system/#comments</comments>
		<pubDate>Thu, 18 Mar 2010 15:23:14 +0000</pubDate>
		<dc:creator>Rob Searles</dc:creator>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[Tutorials]]></category>

		<guid isPermaLink="false">http://www.robsearles.com/?p=292</guid>
		<description><![CDATA[Today I installed the base system on my perfect desktop.]]></description>
			<content:encoded><![CDATA[<p><img class="alignright size-full wp-image-296" title="Arch Linux logo" src="http://www.robsearles.com/wp-content/uploads/2010/03/arch-linux-logo1.png" alt="arch-linux-logo" width="200" height="168" />So today is the day I start building <a href="http://www.robsearles.com/2010/03/16/my-perfect-desktop-day-0/">my perfect system</a>. Before getting going,  I had a good think about <a href="http://distrowatch.com/">which distro</a> to use for the base install. I&#8217;ve been using <a href="http://www.ubuntu.com/">Ubuntu</a> for about 2 years now and love the ease of package management with <a href="http://ubuntuforums.org/showthread.php?t=447386">apt and aptitude</a>. I had a bit of a <a href="http://www.robsearles.com/2010/03/17/installing-php5-3-on-ubuntu-karmic/">play around with Ubuntu Karmic yesterday</a> and I must say it looks fantastic, and with Lucid Lynx only a <a href="https://wiki.ubuntu.com/LucidReleaseSchedule">few weeks away</a> it might come as a bit of a surprise to discover that my choice is a distro I&#8217;ve never used before: <a href="http://www.archlinux.org">Arch</a>.</p>
<p><strong>Why Arch?</strong></p>
<p>There are three main reasons I&#8217;ve picked Arch over Ubuntu, or even <a href="http://www.debian.org/">Debian</a>.</p>
<ol>
<li>I really like the idea of Arch&#8217;s rolling release system. My laptop is currently running Jaunty and in order to get the latest packages I would have to upgrade to Karmic. With Arch this is less of an issue.</li>
<li>As mentioned in my initial post, the base system should be small, quick and light. With Arch you can pick and choose exactly what you want at installation time, keeping it bloat free.</li>
<li>Finally, Arch will be something new to play with!</li>
</ol>
<p>After <a href="http://www.archlinux.org/download/">downloading</a> and burning a Net Install ISO I followed the excellent <a href="http://wiki.archlinux.org/index.php/Official_Arch_Linux_Install_Guide">installation guide in the Arch Wiki</a>.</p>
<p>Installation was relatively painless, the only two slight issues being:</p>
<p>I chose Auto prepare for partitioning, but I didn&#8217;t really know what filesystem to choose. Eventually I opted for Ext4, with so far, no ill effects.</p>
<p>During Boot Device Selection, again I wasn&#8217;t really sure what to do, but after some Googling I <a href="http://news.softpedia.com/news/How-to-Install-Arch-Linux-59239.shtml">found this page</a>, whose advice I followed, but just pressing enter.</p>
<p>I restarted and was greeted with a shell prompt. I quickly pinged Google and my network was still up and running through the ethernet on DHCP.</p>
<p>The next step up a non-root user and X. Again, the Arch <a href="http://wiki.archlinux.org/index.php/Beginners'_Guide">documentation</a> is excellent.</p>
<p>Setting up X was relatively simple. I originally decided to not opt for hotplugging, which was a mistake because I only have a USB keyboard and mouse, neither of which I could use in X<em> (muppet of the day award goes to me)</em>. After turning off the machine and setting up hotplugging, all was fine!</p>
<p>I have been using the <a href="http://awesome.naquadah.org/">Awesome window manager</a> for about a month now. Awesome is a tiling window manager which is heavily geared towards being keyboard friendly, and as it&#8217;s name suggests, it is pretty awesome.</p>
<p>Unlike Jaunty, installing the latest stable version of Awesome was a breeze, I simply ran:</p>

<div class="wp_syntax"><div class="code"><pre class="bash bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;"># pacman -S awesome</span></pre></div></div>

<p>and edited my ~/.xinitrc file adding:</p>

<div class="wp_syntax"><div class="code"><pre class="bash bash" style="font-family:monospace;"><span style="color: #7a0874; font-weight: bold;">exec</span> awesome</pre></div></div>

<p>at the bottom. When I started X again, Awesome was there without any problem.</p>
<p>Sound was equally as simple to install, which was a nice surprise.</p>
<p>So my base system is now setup, with my window manager of choice in place and sound ready and waiting for my music collection. And all this running under 100MB of RAM!</p>
<p>After my first foray into the world of Arch I&#8217;m pretty impressed. <a href="http://wiki.archlinux.org/index.php/Pacman">Pacman</a> is very straight forward to use, after a bit of tweaking here and there it has just worked.</p>
<p>Next time, file management tools.</p>
<img src="http://www.robsearles.com/?ak_action=api_record_view&id=292&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://www.robsearles.com/2010/03/18/my-perfect-desktop-day-1-the-base-system/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

