<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">

 <title>hecticjeff</title>
 <link href="http://hecticjeff.net/index.xml" rel="self"/>
 <link href="http://hecticjeff.net/"/>
 <updated>2012-04-25T12:29:44-07:00</updated>
 <id>http://hecticjeff.net/</id>
 <author>
   <name>Chris Mytton</name>
   <email>self@hecticjeff.net</email>
 </author>

 
 <entry>
   <title>Large-scale JavaScript Application Architecture</title>
   <link href="http://hecticjeff.net/2012/02/11/link-javascript-application-architecture/"/>
   <updated>2012-02-11T00:00:00-08:00</updated>
   <id>http://hecticjeff.net/2012/02/11/link-javascript-application-architecture</id>
   <content type="html">&lt;p&gt;Interesting talk about organising javascript in more complex
applications.&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>Resque Rails Auth</title>
   <link href="http://hecticjeff.net/2012/01/13/resque-rails-auth/"/>
   <updated>2012-01-13T00:00:00-08:00</updated>
   <id>http://hecticjeff.net/2012/01/13/resque-rails-auth</id>
   <content type="html">&lt;p&gt;Recently I found myself wanting to access the resque-web ui on a live
application. I had considered just running resque-web as a separate process,
but after reading &lt;a href=&quot;http://blog.kiskolabs.com/post/776939029/rails3-resque-devise&quot;&gt;this article&lt;/a&gt;
I realised that I could mount resque directly in the router, awesome!&lt;/p&gt;

&lt;p&gt;However, the application doesn't use devise for authentication, so I wanted an
easy way to restrict resque-web to admins.&lt;/p&gt;

&lt;p&gt;Using rails 3 router's &lt;a href=&quot;http://guides.rubyonrails.org/routing.html#advanced-constraints&quot;&gt;advanced constraints&lt;/a&gt;
you can pass a &lt;code&gt;:constraints&lt;/code&gt; options with an object that responds to
&lt;code&gt;matches?&lt;/code&gt; and receives the current request as an argument.&lt;/p&gt;

&lt;p&gt;Since the current user's id is stored in the session, we can simply
retrieve the user and check if they're an admin.&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;ruby&quot;&gt;&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;AdminRestriction&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;matches?&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;request&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;user_id&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;request&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;env&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;rack.session&amp;#39;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;][&lt;/span&gt;&lt;span class=&quot;ss&quot;&gt;:user_id&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;]&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;user&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;User&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;find_by_id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;user_id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;user&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;user&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;admin?&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;

&lt;span class=&quot;no&quot;&gt;MyApplication&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;no&quot;&gt;Application&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;routes&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;draw&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;do&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;mount&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;Resque&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;no&quot;&gt;Server&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;/resque&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;:constraints&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;AdminRestriction&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;# Other application routes.&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;The &lt;code&gt;AdminRestriction&lt;/code&gt; class performs the actual checks, in the router
it is simply passed as a constraint.&lt;/p&gt;

&lt;p&gt;First we pull the &lt;code&gt;user_id&lt;/code&gt; out of the session, then we attempt to get
the user from the database. Finally we check that we've found a user and
that they are an admin.&lt;/p&gt;

&lt;p&gt;If the user tries to access &lt;code&gt;/resque&lt;/code&gt; and they are not an admin, they
simply get a 404 error.&lt;/p&gt;

&lt;p&gt;This technique can be used with any rack application, or indeed with any
regular route, just pass a &lt;code&gt;:constraints&lt;/code&gt; option (see the
&lt;a href=&quot;http://api.rubyonrails.org/classes/ActionDispatch/Routing/Mapper/Base.html#method-i-match&quot;&gt;match method docs&lt;/a&gt;),
and the constraints that you apply can use any part of the request to decide if
it matches. You can restrict access by ip address, or do routing based
on the request's geolocation.&lt;/p&gt;

&lt;p&gt;The possibilities are endless.&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>Test Driven Development Lifecycle</title>
   <link href="http://hecticjeff.net/2011/03/22/test-driven-development-lifecycle/"/>
   <updated>2011-03-22T00:00:00-07:00</updated>
   <id>http://hecticjeff.net/2011/03/22/test-driven-development-lifecycle</id>
   <content type="html">&lt;p&gt;Start at the top level with a user requirement. This will ensure that
you are trying to solve the right problem in the first place.&lt;/p&gt;

&lt;h2&gt;Cucumber / Steak&lt;/h2&gt;

&lt;p&gt;Write a simple high level requirement in cucumber or steak. For example
if you were writing an api and you wanted to allow people to post
activity.&lt;/p&gt;

&lt;p&gt;First you would write out the requirement in plain english.&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;gherkin&quot;&gt;&lt;span class=&quot;k&quot;&gt;Feature:&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt; Creating activity&lt;/span&gt;
&lt;span class=&quot;nf&quot;&gt;  As an activity producer&lt;/span&gt;
&lt;span class=&quot;nf&quot;&gt;  I want to post activity to the api&lt;/span&gt;
&lt;span class=&quot;nf&quot;&gt;  In order to share it with the world&lt;/span&gt;

&lt;span class=&quot;nf&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;Scenario:&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt; POST to /activity&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;    Given &lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;I am an authorized activity producer&lt;/span&gt;
&lt;span class=&quot;nf&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;When &lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;I post some activity&lt;/span&gt;
&lt;span class=&quot;nf&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;Then &lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;I should receive a &lt;/span&gt;&lt;span class=&quot;s&quot;&gt;200&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt; success status&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;This defines the high level picture of what the application aims to
achieve. From this point the next step is to write some steps to
properly test that the features are working.&lt;/p&gt;

&lt;p&gt;Obviously the features won't be working yet, because that's not how
test/behaviour/real-world driven development works. If you start writing
code before you really know what you are building, then it is likely
that you will at some point, perhaps without realising, implement the
wrong feature. More likely though, being that smart martin that you
are, you will implement the correct feature, but unwittingly leave a bug
in it. No harm, you say, bugs happen in software and it's probably only
one line of code that needs changing. But if the bug doesn't manifest
itself for a few days, weeks, months, then it will take an ever
increasing amount of time for you to track it down. Then once you have
isolated it, you have to ensure it won't happen again.&lt;/p&gt;

&lt;p&gt;This all comes down to testing. If you can easily run a bank of tests
that confirm that your application is behaving at night you will,
infact, sleep better at night. For some this is reason enough to write
tests, but wait, there's more. If you have a comprehensive collection of
tests you can use to verify that your application is working correctly,
then you are in a very good position to do some refactoring. No more
worrying if you've broken some seemingly unrelated piece of
functionality when you make a change to the application.&lt;/p&gt;

&lt;h2&gt;Controller tests&lt;/h2&gt;

&lt;p&gt;The next layer down the stack is the controller, this makes sense, since
this is the layer that manages incoming requests. So after writing the
initial acceptance test in a high level language, then implementing the
steps necessary to test this logic in a slightly lower level language,
you now drop down to a relativly granular level. This is where you start
to stub out the main functionality of the application. So in the case of
our activity example above, you would at this stage be stubbing out the
functionality of the models, and just dealong with the way that
controllers handle requests.&lt;/p&gt;

&lt;p&gt;This layer may be preceeded by another, slightly higher level one in
which the routing for the application is set up, however this is quite a
web application specific area.&lt;/p&gt;

&lt;h2&gt;Model tests&lt;/h2&gt;

&lt;p&gt;The model tests are the core of the testing universe, they are very
small, low level details about the various methods that a model
provides.&lt;/p&gt;

&lt;p&gt;The model tests are the most important tests, as they contain the
business logic for your application. But they are often also the easiest
tests to write. This is because a lot of the time you will be dealing
with primitive data types. No external services to worry about. This is
a good reason to push as much logic into the model as possible, it is
far easier to test this that it is to put it in your controller then try
to stub it out.&lt;/p&gt;

&lt;h2&gt;Other tests&lt;/h2&gt;

&lt;p&gt;All of this testing malarkey is great and everything, but sometimes you
have got to just use the product to uncover random bugs, but don't
worry, that's not to say we can't still use testing to help us. Instead
of jumping right in and fixing that bug like a good boy scout
hacker, write a failing test that reproduces the bug. This test may
exists as (m)any layer(s) of the test stack. Often you will need to
write a test in the acceptance layer to perform the same interaction
that caused the bug, then once that has caught the bug, go down the
layers and write tests for the code paths that the bug touches, these
failing tests will then (all going to plan) be green once you have fixed
the bug, and as a billy bonus you've got yourself another test or two
that will ensure many good nights sleep in the future.&lt;/p&gt;

&lt;h2&gt;Final thoughts&lt;/h2&gt;

&lt;p&gt;The testing pattern of development is still not used as a default
development technique. Many (most) tutorials you find out there will
focus on the functionality that they are trying to tutor you on, but a
vital part of implementing any functionality is to ensure a long
lifespan by testing it thoroughly.&lt;/p&gt;

&lt;p&gt;Testing will seem like a chore at first, but once you get the hang of
it, the whole process becomes a pattern, that if followed with a small
bit of dicipline, will lead you to enlightenment, it will allow you to
model your ideas with more structure with the tools available.&lt;/p&gt;

&lt;p&gt;In many ways it's like learning a new language, with more languages, you
see more ways of doing things. With testing, you'll see new ways to
express your applications logic using high level language.&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>hecticjeff.net</title>
   <link href="http://hecticjeff.net/2010/06/04/hecticjeffnet/"/>
   <updated>2010-06-04T00:00:00-07:00</updated>
   <id>http://hecticjeff.net/2010/06/04/hecticjeffnet</id>
   <content type="html">&lt;p&gt;This blog now lives at a new address, &lt;a href=&quot;http://hecticjeff.net/&quot;&gt;hecticjeff.net&lt;/a&gt;. If you're reading this then you are already here! I'm still using &lt;a href=&quot;http://heroku.com/&quot;&gt;heroku&lt;/a&gt; to host the blog, it's quite simply the best (only?) way to host ruby apps in the cloud. If you want to get in touch you can now email me on &lt;strong&gt;self at hecticjeff dot net&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;The blog is going to get some love soon, comments will be the first thing to activate, as I'm using the excellent &lt;a href=&quot;http://cloudhead.io/toto&quot;&gt;toto&lt;/a&gt; from cloudhead I will most likely opt for the wonderful &lt;a href=&quot;http://disqus.com/&quot;&gt;Disqus&lt;/a&gt; comment system. That's priority 1. Priority 0 is to write some more posts, but I've been settling into a new job (with the awesome &lt;a href=&quot;http://www.simpleweb.co.uk/&quot;&gt;Simpleweb&lt;/a&gt; crew) in a new city (Bristol) recently, so this blog has been neglected to say the least. But it's time has come.&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>An introduction to node.js</title>
   <link href="http://hecticjeff.net/2010/02/14/an-introduction-to-nodejs/"/>
   <updated>2010-02-14T00:00:00-08:00</updated>
   <id>http://hecticjeff.net/2010/02/14/an-introduction-to-nodejs</id>
   <content type="html">&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Update&lt;/strong&gt;: The node.js api is in constant flux until it reaches a 1.0 release. The code in this post doesn't work with the latest releases of node (it was written for the 0.1.3x series), however the concept is still the same. The best place to start is the &lt;a href=&quot;http://nodejs.org/api.html&quot;&gt;node.js api docs&lt;/a&gt;.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;I've only just got round to doing some coding with &lt;a href=&quot;http://tinyclouds.org/&quot;&gt;Ryan Dahl's&lt;/a&gt; great &lt;a href=&quot;http://nodejs.org/&quot;&gt;node.js&lt;/a&gt; project, although the project has been
around for about a year now, so I've put together this short introduction to give you a taste of what node is about.&lt;/p&gt;

&lt;p&gt;The project is under heavy development, and the api is still changing quite regularly, but don't let that scare you off, node is stable and usable now.&lt;/p&gt;

&lt;p&gt;For those that don't know, &lt;a href=&quot;http://nodejs.org/&quot;&gt;node.js&lt;/a&gt; is a project that brings javascript to the server and into the realm of PHP and Ruby. However with node there is one big difference. It is based around the concept of events, just like in the browser, javascript waits for events to be fired, then invokes the function that has been attached to the event.&lt;/p&gt;

&lt;p&gt;Node uses this to it's full advantage, most operations that you perform in node will have a callback associated with it, this means that node doesn't have to wait while it's completing some time-consuming task, it can go about serving other requests, then when the event is fired it can go back and execute the function attached to the event. This leads to very fast response times when using node as a web server, as it can handle thousands of requests per second.&lt;/p&gt;

&lt;p&gt;The program can be installed easily on OS X (using &lt;a href=&quot;http://github.com/mxcl/homebrew&quot;&gt;Homebrew&lt;/a&gt; or compile from &lt;a href=&quot;http://github.com/ry/node&quot;&gt;source&lt;/a&gt;) and Linux (compile from
&lt;a href=&quot;http://github.com/ry/node&quot;&gt;source&lt;/a&gt;), Windows support is planned in the future, but is currently non-existent. Once installed you have access to a
command line utility, &lt;code&gt;node&lt;/code&gt;, this runs .js files through the node interpreter.&lt;/p&gt;

&lt;p&gt;A basic node hello world program could look like this:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;javascript&quot;&gt;&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;sys&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;require&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;sys&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;sayHello&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;Hello &amp;#39;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;name&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;!&amp;#39;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;nx&quot;&gt;sys&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;puts&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;sayHello&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;World&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;save this into a file and then run it from the command line like so:&lt;/p&gt;

&lt;p&gt;As you can see it's just javascript syntax, just like you would use in a browser, but the first difference the astute reader will
have noticed is the require() statement. This is built into node and conforms to the &lt;a href=&quot;http://commonjs.org/&quot;&gt;CommonJS&lt;/a&gt; Module specification. This
version of require is slightly different from one you might see in Ruby or PHP, it actually returns an object which you can assign
to a variable, or use directly:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;javascript&quot;&gt;&lt;span class=&quot;nx&quot;&gt;require&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;sys&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;puts&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;Hello World!&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;Each module is a self contained unit of code, it has its own private scope, so can define functions to use internally, then expose public functions to be used externally using the exports object. Here's an example of a simple module:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;javascript&quot;&gt;&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;calculator&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;nx&quot;&gt;add&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;b&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;},&lt;/span&gt;
  &lt;span class=&quot;nx&quot;&gt;subtract&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;b&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;},&lt;/span&gt;
  &lt;span class=&quot;nx&quot;&gt;multiply&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;b&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;},&lt;/span&gt;
  &lt;span class=&quot;nx&quot;&gt;divide&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;b&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;nx&quot;&gt;process&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;mixin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;exports&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;calculator&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;The last line may seem a little strange, but all it does is extend the exports object with the properties defined in the calculator object, if that line wasn't there then the module wouldn't return anything as it wouldn't export anything. To use our new (rather basic) module we'd do something like this:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;javascript&quot;&gt;&lt;span class=&quot;c1&quot;&gt;// Assuming that calc.js is in the same dir as this file&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;calc&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;require&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;./calc&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;// Now we can use the calc object to do calculations, joy!&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;ten&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;calc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;add&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;7&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;two&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;calc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;subtract&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;This overview barely scratches the surface of what is possible with node.js, the &lt;a href=&quot;http://nodejs.org/api.html&quot;&gt;api documentation&lt;/a&gt; contains all you need to know about node, and it is all on one (albeit rather long) page, so once you've read through it you should have a pretty good idea how you'd go about coding for it.&lt;/p&gt;

&lt;p&gt;There are already some very interesting projects using node available to try. A quick glance at the &lt;a href=&quot;http://wiki.github.com/ry/node/&quot;&gt;node wiki&lt;/a&gt; will give you a glimpse of what is possible with this fantastic new technology, as well as some more background information on the project.&lt;/p&gt;
</content>
 </entry>
 

</feed>

