<?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; JavaScript</title>
	<atom:link href="http://www.robsearles.com/category/javascript/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>Mon, 31 May 2010 15:44:09 +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 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>0</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>6</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>1</slash:comments>
		</item>
		<item>
		<title>Hacking about with Node.JS</title>
		<link>http://www.robsearles.com/2010/03/28/hacking-about-with-node-js/</link>
		<comments>http://www.robsearles.com/2010/03/28/hacking-about-with-node-js/#comments</comments>
		<pubDate>Sun, 28 Mar 2010 13:41:10 +0000</pubDate>
		<dc:creator>Rob Searles</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[nodejs]]></category>

		<guid isPermaLink="false">http://www.robsearles.com/?p=301</guid>
		<description><![CDATA[A happy weekend hacking about with Node.JS]]></description>
			<content:encoded><![CDATA[<p><strong>Update:</strong> If you are looking for a <a href="../2010/05/28/nodejs-tutorial-with-couchdb-and-haml-erdnodeflips/">NodeJS  tutorial</a>, visit this post: <a href="../2010/05/28/nodejs-tutorial-with-couchdb-and-haml-erdnodeflips/">NodeJS  Tutorial with CouchDB and Haml</a>.</p>
<p>After proclaiming that <a href="http://nodejs.org/">Node.JS</a> is <a href="http://www.robsearles.com/2009/11/29/first-steps-with-node-js-exciting-stuff/">exciting stuff</a>, today was the first time I&#8217;ve actually had the time to play around with it properly. I&#8217;ve been thinking about writing a custom FTP server for a project that we&#8217;re working on at the moment, and instead of starting from scratch, I thought I&#8217;d see if anyone else has tried to do the same. Fortunately I found <a href="http://blog.beardsoft.com/node-ftp-server-initial-release">Andrew Johnston started an FTP server</a> only a few months ago: <a href="http://github.com/billywhizz/nodeftpd">NodeFTPd on GitHub</a>. I quickly created a clone and tried it out. It didn&#8217;t work! Node.JS is such a fast moving target that a lot of the API has been changed in the 3 months since Andrew initially released nodeftpd.</p>
<p>Perfect! This meant that I could play around with Node!</p>
<p>I&#8217;ve been hacking about on it for 4 hours or so, you can see <a href="http://github.com/ibrow/nodeftpd">my results at GitHub</a>.</p>
<p>So what have my 4 hours of hacking around taught me about Node? Well, it&#8217;s not just exciting, it also bloody good fun!</p>
<img src="http://www.robsearles.com/?ak_action=api_record_view&id=301&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://www.robsearles.com/2010/03/28/hacking-about-with-node-js/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>IE 7 indexOf() Replacement</title>
		<link>http://www.robsearles.com/2010/03/11/ie-7-indexof-replacement/</link>
		<comments>http://www.robsearles.com/2010/03/11/ie-7-indexof-replacement/#comments</comments>
		<pubDate>Thu, 11 Mar 2010 16:51:07 +0000</pubDate>
		<dc:creator>Rob Searles</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[jQuery]]></category>

		<guid isPermaLink="false">http://www.robsearles.com/?p=267</guid>
		<description><![CDATA[Everyday I hate IE a little bit more, and by the same token, everyday I love jQuery that little bit more.
The reason for my hate/love feelings today was due to a bug caused by IE 7 not supporting the indexOf() method. I have no idea why IE doesn&#8217;t support this method, but it is annoying [...]]]></description>
			<content:encoded><![CDATA[<p>Everyday <a href="http://www.ihateie.com/">I hate IE</a> a little bit more, and by the same token, everyday I love <a href="http://jquery.com/">jQuery</a> that little bit more.</p>
<p>The reason for my hate/love feelings today was due to a bug caused by IE 7 not supporting the <a href="https://developer.mozilla.org/En/Core_JavaScript_1.5_Reference/Objects/Array/IndexOf">indexOf()</a> method. I have no idea why IE doesn&#8217;t support this method, but it is annoying never-the-less. However, there is an easy fix if you are using jQuery &#8211; the <a href="http://api.jquery.com/jQuery.inArray/">inArray()</a> function.</p>
<p>The inArray() function is pretty simple to use.</p>
<p>Instead of having:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript javascript" style="font-family:monospace;">haystack_array.<span style="color: #660066;">indexOf</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;needle&quot;</span><span style="color: #009900;">&#41;</span></pre></div></div>

<p>You should use:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript javascript" style="font-family:monospace;">jQuery.<span style="color: #660066;">inArray</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;needle&quot;</span><span style="color: #339933;">,</span> haystack_array<span style="color: #009900;">&#41;</span></pre></div></div>

<p>For those of you crazy nutters that aren&#8217;t using jQuery, there is a <a href="http://soledadpenades.com/2007/05/17/arrayindexof-in-internet-explorer/">good post on this blog</a> that will help you out.</p>
<img src="http://www.robsearles.com/?ak_action=api_record_view&id=267&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://www.robsearles.com/2010/03/11/ie-7-indexof-replacement/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>First Steps with Node.js: exciting stuff</title>
		<link>http://www.robsearles.com/2009/11/29/first-steps-with-node-js-exciting-stuff/</link>
		<comments>http://www.robsearles.com/2009/11/29/first-steps-with-node-js-exciting-stuff/#comments</comments>
		<pubDate>Sun, 29 Nov 2009 12:28:02 +0000</pubDate>
		<dc:creator>Rob Searles</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Opinion]]></category>
		<category><![CDATA[nodejs]]></category>

		<guid isPermaLink="false">http://www.robsearles.com/?p=231</guid>
		<description><![CDATA[Ryan Dahl's Node.js is very exciting stuff, with lots of potential. Something to keep close track of. ]]></description>
			<content:encoded><![CDATA[<div style="background: #FFFEEB; margin: 10px 0 0 0; padding: 5px 10px; border: 1px solid #ccc;">
If you are looking for a <a href="http://www.robsearles.com/2010/05/28/nodejs-tutorial-with-couchdb-and-haml-erdnodeflips/">NodeJS tutorial</a>, visit this post: <a href="http://www.robsearles.com/2010/05/28/nodejs-tutorial-with-couchdb-and-haml-erdnodeflips/">NodeJS Tutorial with CouchDB and Haml</a>.</div>
<p>During my <a href="http://www.robsearles.com/2009/11/28/3-days-without-the-interwebs/">Saturday morning reading yesterday</a> I fell over something called <a href="http://nodejs.org/">Node.js</a>.  According to the website</p>
<blockquote><p>Node&#8217;s goal is to provide an easy way to build scalable network         programs.</p></blockquote>
<p>which is kind of exciting, but not mind blowing, however, Ryan Dahl&#8217;s <a href="http://github.com/ry/node">GitHub page</a> describes it as</p>
<blockquote><p><span id="repository_description">evented I/O for v8 javascript</span></p></blockquote>
<p><span>Which is slightly more exciting. However, it is when you start to play around with it that things begin to get very exciting indeed.</span></p>
<p><span><span id="more-231"></span></span></p>
<p><a href="http://simonwillison.net/2009/Nov/23/node/ ">Simon Willison&#8217;s latest blog post</a> was the first I&#8217;d heard of Node and he offers a very good introduction and tutorial, bringing in a Djanjo slant. Towards the end of the post he mentions database connectivity and when I think about the possibilities with <a href="http://couchdb.apache.org/">CouchDB</a> my mind explodes with possibilities!</p>
<p>Over at Naked Javascript, there is <a href="http://www.nakedjavascript.com/going-evented-with-nodejs">another great post</a>, offering a more detailed tutorial, including a client that gets a Twitter search. Again, this article is very enthused about Node and its future direction.</p>
<p>Ryan Dahl&#8217;s presentation on Node is hugely compelling, and provides excellent insights into the philosophy behind Node: <a href="http://s3.amazonaws.com/four.livejournal/20091117/jsconf.pdf">PDF download</a>.</p>
<p>With all this excitement in the air, I thought I better download me a copy and see what all the fuss is about.</p>
<p>Downloading and installing both <a href="http://code.google.com/apis/v8/.">Google&#8217;s V8 JavaScript Engine</a> (which Node is built upon) and Node was effortless. I won&#8217;t go into the details of installation here as it is covered in both the articles above and the Node homepage, needless to say that and I had them up and running on a clean Debian <a href="http://www.virtualbox.org/">VirtualBox</a> install in about 5 &#8211; 10 minutes.</p>
<p>I copied and pasted the &#8220;Hello World&#8221; example from Simon Willison&#8217;s article, ran it and I was away.</p>
<p>The code used is extremely simple, as you can see</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>   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;
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;">sendHeader</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;">sendBody</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'Hello World'</span><span style="color: #009900;">&#41;</span>;
 res.<span style="color: #660066;">finish</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>.<span style="color: #660066;">listen</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">8000</span><span style="color: #009900;">&#41;</span>;
&nbsp;
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>To run I simply ran</p>

<div class="wp_syntax"><div class="code"><pre class="bash bash" style="font-family:monospace;">$ node hello.js
Server running at http:<span style="color: #000000; font-weight: bold;">//</span>127.0.0.1:<span style="color: #000000;">8000</span><span style="color: #000000; font-weight: bold;">/</span></pre></div></div>

<p>and then navigated to the page in my browser where I was greeted with a rather plain but not boring &#8220;Hello World&#8221;</p>
<p>The res.finish() call tells the client that the server has finished and it should consider the message complete.</p>
<p>This started me thinking, it would be possible to complete all the stuff that needs to be sent to the client, finish the conversation with the client, but then still carry on, without losing any efficiency. So I hacked apart the &#8220;hello world&#8221; app to see what happened if you did stuff after the send. The new code 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>   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: #003366; font-weight: bold;">var</span> test_func <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>
 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>
 sys.<span style="color: #660066;">puts</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'Called func'</span><span style="color: #009900;">&#41;</span>;
 <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">5000</span><span style="color: #009900;">&#41;</span>;
<span style="color: #009900;">&#125;</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;">sendHeader</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;">sendBody</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'Hello World'</span><span style="color: #009900;">&#41;</span>;
 res.<span style="color: #660066;">finish</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;
 test_func<span style="color: #009900;">&#40;</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>;
&nbsp;
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>I added the timeout to represent time needed to execute other processes, such as connecting to a  database etc. It is heavily exaggerated but it should give me an idea what&#8217;s going on. Running the program now starts off as before:</p>

<div class="wp_syntax"><div class="code"><pre class="bash bash" style="font-family:monospace;">$ node hello.js
Server running at http:<span style="color: #000000; font-weight: bold;">//</span>127.0.0.1:<span style="color: #000000;">8000</span><span style="color: #000000; font-weight: bold;">/</span></pre></div></div>

<p>But then when navigating to the page, 5 seconds later the command line reported:</p>

<div class="wp_syntax"><div class="code"><pre class="bash bash" style="font-family:monospace;">Called func</pre></div></div>

<p>So it is clearly completing the conversation with the client first, then executing the function.</p>
<p>I have played around with <a href="http://httpd.apache.org/docs/2.0/programs/ab.html">Apache Benchmarking</a> to see how this handles large requests, and it seems to hold up, but obviously more stringent tests are required before including this technology into a production environment.</p>
<p>However, this is still very exciting. It opens up the possibility of having an HTTP server that waits for requests, getting the key information and saying &#8220;bye&#8221; before processing the request. Something like a queue server, but very fast and very efficient. And when coupled with CouchDB or similar, well, things start to get very interesting.</p>
<p>So I am going to spend the rest of this weekend hacking around with Node, I want to know everything about it, and you should too.</p>
<p>Node is definitely something to keep an eye on.</p>
<p><strong>Update</strong>: Just found another <a href="http://britg.com/2009/07/01/server-side-javascript-continued-node-js-plus-example/">good blog post</a>, this time with an example for Long Polling.</p>
<p><strong>Update:</strong> If you are looking for a <a href="../2010/05/28/nodejs-tutorial-with-couchdb-and-haml-erdnodeflips/">NodeJS  tutorial</a>, visit this post: <a href="../2010/05/28/nodejs-tutorial-with-couchdb-and-haml-erdnodeflips/">NodeJS  Tutorial with CouchDB and Haml</a>.</p>
<img src="http://www.robsearles.com/?ak_action=api_record_view&id=231&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://www.robsearles.com/2009/11/29/first-steps-with-node-js-exciting-stuff/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Fixed &#8211; jQuery DatePicker not working in Thickbox</title>
		<link>http://www.robsearles.com/2009/01/27/fixed-jquery-datepicker-not-working-in-thickbox/</link>
		<comments>http://www.robsearles.com/2009/01/27/fixed-jquery-datepicker-not-working-in-thickbox/#comments</comments>
		<pubDate>Tue, 27 Jan 2009 11:55:39 +0000</pubDate>
		<dc:creator>Rob Searles</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[jQuery]]></category>

		<guid isPermaLink="false">http://www.robsearles.co.uk/?p=66</guid>
		<description><![CDATA[The jQuery DatePicker plugin was not working when inside a Thickbox. After some Googling, managed to solve the problem.]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been struggling today for about an hour trying to get <a href="http://jquery.com" target="_blank">jQuery</a>&#8217;s <a href="http://www.kelvinluck.com/assets/jquery/datePicker/" target="_blank">DatePicker</a> working in <a href="http://jquery.com/demo/thickbox/" target="_blank">Thickbox</a>. There appeared to be some kind of conflict. I tried hacking up the Datepicker plugin file, changing the z-index of the CSS but nothing. The date picker wasn&#8217;t even being called.</p>
<p>Eventually I found that other people were having the <a href="http://code.google.com/p/jquery-datepicker/issues/detail?id=24" target="_blank">same problem</a>, and a <a href="http://code.google.com/p/jquery-datepicker/issues/detail?id=24#c5" target="_blank">comment by Kevin Luck</a> (the author of DatePicker) flicked my brain into gear.</p>
<p>The problem was that I was initialising the datepicker on page load, not on the loading of the thickbox content.</p>
<p>For example, at the start of the page the Thickbox was going to be loaded in I had this:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript javascript" style="font-family:monospace;"><span style="color: #339933;">&lt;</span>script type<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;text/javascript&quot;</span><span style="color: #339933;">&gt;</span>
	$<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;">// initiate date picker</span>
		$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'.pick-date'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">datePicker</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>;
<span style="color: #339933;">&lt;/</span>script<span style="color: #339933;">&gt;</span>
&lt;/head&gt;</pre></div></div>

<p>However, I found that if I removed that from the head, and instead inserted it in the Thickbox content, e.g:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript javascript" style="font-family:monospace;">&lt;div id=&quot;MyThickboxContent&quot;&gt;
<span style="color: #339933;">&lt;</span>script type<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;text/javascript&quot;</span><span style="color: #339933;">&gt;</span>
	$<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;">// initiate date picker</span>
		$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'.pick-date'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">datePicker</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>;
<span style="color: #339933;">&lt;/</span>script<span style="color: #339933;">&gt;</span></pre></div></div>

<p>It worked fine, problem solved! </p>
<img src="http://www.robsearles.com/?ak_action=api_record_view&id=66&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://www.robsearles.com/2009/01/27/fixed-jquery-datepicker-not-working-in-thickbox/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>jQuery: Link override with search box</title>
		<link>http://www.robsearles.com/2008/11/26/jquery-link-override-with-search-box/</link>
		<comments>http://www.robsearles.com/2008/11/26/jquery-link-override-with-search-box/#comments</comments>
		<pubDate>Wed, 26 Nov 2008 19:26:35 +0000</pubDate>
		<dc:creator>Rob Searles</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[jQuery]]></category>

		<guid isPermaLink="false">http://www.robsearles.co.uk/?p=16</guid>
		<description><![CDATA[ 

Just been working on a little something for a client involving Google Custom Search Engines.
The app we&#8217;re building uses 4 different search engines which are all specialised in a certain area. There is a central &#8220;Overview&#8221; page that gets the top results for the search from each of the search engines, plus there are [...]]]></description>
			<content:encoded><![CDATA[<p><script src="/javascript/jquery.1.2.6.js" type="text/javascript"></script> <script src="/javascript/jquery.url.js" type="text/javascript"></script><br />
<script type="text/javascript"><!--
$(function(){
	$("#jQuerySearchExample a").click(function () {
		var path = "http://www.google.co.uk/";
		path += jQuery.url.setUrl($(this).attr("href")).attr("path");
		path += "?"+$("#jQuerySearchExampleInput").serialize();
		$(this).attr("href", path);
	});
});
// --></script></p>
<p>Just been working on a little something for a client involving <a href="http://www.google.com/coop/cse/" target="_blank">Google Custom Search</a> Engines.</p>
<p>The app we&#8217;re building uses 4 different search engines which are all specialised in a certain area. There is a central &#8220;Overview&#8221; page that gets the top results for the search from each of the search engines, plus there are normal links above the search box that, when clicked on should take the user directly to the Custom Search Engine results page for the string in the search box.</p>
<p>Sounds quite straightforward, but the links also save the current search (which may be different to the search box if the user has just entered a search into the box) so in this case, the old search needs to be stripped and the new search carried forward to the Custom Search Engine.</p>
<p>So it&#8217;s a little more tricky, but fortunately using <a href="http://jquery.com" target="_blank">jQuery</a> and the <a href="http://allmarkedup.com/archives/jquery-url-parser-v10/" target="_blank">URL Parser plugin</a> by <a href="http://allmarkedup.com/" target="_blank">Mark Perkins</a> I was able to cut out most of the tricky stuff.</p>
<p>Below is a little demo to illustrate the main part of the jQuery usage. Before you conduct a search, you should be able to click on the links to go and search Google for those keywords (jQuery, CakePHP, Ruby On Rails and No Search conducted). If you enter a string in the box and hit the &#8220;Search&#8221; button, you&#8217;ll go to Google and search for the string. However, if you enter a string in the search box but then click on a link, you&#8217;ll be taken to Google and search for the string.</p>
<p>This isn&#8217;t quite how it works on the client site, but is enough to show how the jQuery is being used.</p>
<ul id="jQuerySearchExample">
<li><a href="http://www.google.co.uk/search" target="_blank">No Search</a></li>
<li><a href="http://www.google.co.uk/search?q=jquery" target="_blank">jQuery</a></li>
<li><a href="http://www.google.co.uk/search?q=CakePHP" target="_blank">CakePHP</a></li>
<li><a href="http://www.google.co.uk/search?q=ruby+on+rails" target="_blank">Ruby On Rail</a></li>
</ul>
<p>Search Google</p>
<form action="http://www.google.com/search" method="get"> </p>
<input id="jQuerySearchExampleInput" name="q" type="text" />
<input type="submit" value="Search" />
</form>
<p>Javascript code in the &lt;head&gt; tags is:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript javascript" style="font-family:monospace;">&lt;script src=&quot;/javascript/jquery.1.2.6.js&quot; type=&quot;text/javascript&quot;&gt;&lt;/script&gt; 
&lt;script src=&quot;/javascript/jquery.url.js&quot; type=&quot;text/javascript&quot;&gt;&lt;/script&gt;
<span style="color: #339933;">&lt;</span>script type<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;text/javascript&quot;</span><span style="color: #339933;">&gt;&lt;!--</span>
$<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: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#jQuerySearchExample a&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">click</span><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: #003366; font-weight: bold;">var</span> path <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;http://www.google.co.uk/&quot;</span>;
		path <span style="color: #339933;">+=</span> jQuery.<span style="color: #660066;">url</span>.<span style="color: #660066;">setUrl</span><span style="color: #009900;">&#40;</span>$<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">attr</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;href&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">attr</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;path&quot;</span><span style="color: #009900;">&#41;</span>;
		path <span style="color: #339933;">+=</span> <span style="color: #3366CC;">&quot;?&quot;</span><span style="color: #339933;">+</span>$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#jQuerySearchExampleInput&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">serialize</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;
		$<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">attr</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;href&quot;</span><span style="color: #339933;">,</span> path<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;">&#41;</span>;
<span style="color: #006600; font-style: italic;">// --&gt;&lt;/script&gt;</span></pre></div></div>

<p>And the HTML is as follows:</p>

<div class="wp_syntax"><div class="code"><pre class="html4strict html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">ul</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;jQuerySearchExample&quot;</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">li</span>&gt;&lt;<span style="color: #000000; font-weight: bold;">a</span> <span style="color: #000066;">href</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;http://www.google.co.uk/search&quot;</span> <span style="color: #000066;">target</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;_blank&quot;</span>&gt;</span>No Search<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">a</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">li</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">li</span>&gt;&lt;<span style="color: #000000; font-weight: bold;">a</span> <span style="color: #000066;">href</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;http://www.google.co.uk/search?q=jquery&quot;</span> <span style="color: #000066;">target</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;_blank&quot;</span>&gt;</span>jQuery<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">a</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">li</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">li</span>&gt;&lt;<span style="color: #000000; font-weight: bold;">a</span> <span style="color: #000066;">href</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;http://www.google.co.uk/search?q=CakePHP&quot;</span> <span style="color: #000066;">target</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;_blank&quot;</span>&gt;</span>CakePHP<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">a</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">li</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">li</span>&gt;&lt;<span style="color: #000000; font-weight: bold;">a</span> <span style="color: #000066;">href</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;http://www.google.co.uk/search?q=ruby+on+rails&quot;</span> <span style="color: #000066;">target</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;_blank&quot;</span>&gt;</span>Ruby On Rail<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">a</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">li</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">ul</span>&gt;</span>
Search Google
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">form</span> <span style="color: #000066;">action</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;http://www.google.com/search&quot;</span> <span style="color: #000066;">method</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;get&quot;</span>&gt;</span> 
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">input</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;jQuerySearchExampleInput&quot;</span> <span style="color: #000066;">name</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;q&quot;</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text&quot;</span> <span style="color: #66cc66;">/</span>&gt;</span> 
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">input</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;submit&quot;</span> <span style="color: #000066;">value</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;Search&quot;</span> <span style="color: #66cc66;">/</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">form</span>&gt;</span></pre></div></div>

<p>As you can see, I use</p>

<div class="wp_syntax"><div class="code"><pre class="javascript javascript" style="font-family:monospace;">jQuery.<span style="color: #660066;">url</span>.<span style="color: #660066;">setUrl</span><span style="color: #009900;">&#40;</span>$<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">attr</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;href&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">attr</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;path&quot;</span><span style="color: #009900;">&#41;</span>;</pre></div></div>

<p>To strip out the current query string from the hard coded URL and then</p>

<div class="wp_syntax"><div class="code"><pre class="javascript javascript" style="font-family:monospace;"><span style="color: #3366CC;">&quot;?&quot;</span><span style="color: #339933;">+</span>$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#jQuerySearchExampleInput&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">serialize</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;</pre></div></div>

<p>To replace it with the string within the search box. </p>
<p>Took a bit of faffing about, but works quite well</p>
<img src="http://www.robsearles.com/?ak_action=api_record_view&id=16&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://www.robsearles.com/2008/11/26/jquery-link-override-with-search-box/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
