I’ve seen a day I never thought would come: the day I turned into a hipster!
If you’ve been following this blog, you’ll have noticed that throughout 2011 I’ve been workingmoreandmorein Javascript, more specifically withNode.JS.
The last few months have been especially Node.JS-centric. During the day, I’ve been building architectural improvements intoJabbakam. Evenings and weekends, I’ve been hacking about on theLive Unsignedmobile site. All of my recent projects revolve around Node.JS, and I’ve been loving it. Node is incredibly interesting and powerful, and it is giving my brain a workout.
However, Javascript itself has a few niggly bits that become more problematic the larger the project gets. Issues with structure and maintainability of code motivate projects such asJooseand Backbone.js. One of the things that has been bugging me is having to hack in class-based inheritance. After a lot of experimentation, I settled onPrototypal Inheritance, which does the job to an extent, but left me dissatisfied .
So, the other day I finally broke down and triedCoffeeScript.
When CoffeeScript first came out I resolved never to bother with it. It is clearly forRuby peoplewho don’t quite understand Javascript. Since then it’s been getting alotofpress, and it now even hasits own book!
Anyway, I was up the other night, waiting for my daughter to wake up in screaming pain as razor-sharp pieces of enamel slowly bored through her gums. With nothing better to do, I decided to play around with CoffeeScript.
I was up and running in no time, installing theNode.JS Moduleand browsing theLittle Book of CoffeeScript. After about an hour I had effortlessly written some additions to Live Unsigned which integrated seamlessly. To be honest, I feel like a bit of a muppet for not trying it sooner.
After just a couple of days playing around with it, CoffeeScript has now become a major part of my toolkit. The syntax is elegant, nicely lending itself to clean coding, and code can be reused easily, improving project structure. CoffeeScript compiles to pretty clean Javascript, and from what I can make out, the better you understand Javascript, the better CoffeeScript you can write.
So yes, I am now coding in CoffeeScript.
I am now a hipster.
There is nothing left for me but to rush out, buy some skinny jeans and a Macbook Air and learn Vim.
Note: This is the last in a series of posts describing my experiments with constructing a Middle End. In Part 1, we discussed the concept of the middle end and its advantages. In Part 2, we constructed a very simple example.
In this post, we’re going to make that example slightly more complex by introducing dependencies within the modules. Now the code must be able to handle these dependencies for both the client side and the server side.
In the last post, we built a simple module consisting of a single function that returned a greeting:
var Greetings =function(){}
Greetings.prototype.hello=function(who){return"Hello "+who;
}
module.exports= Greetings;
At this point, it would be very useful to sanitize and validate the parameter passed to the “hello” function. We could obviously build this functionality directly into the code, but this is exactly the sort of thing that should be abstracted into its own module(s).
For the purpose of this example, we’ll simply include the functionality to give the parameter passed a “trim”; after some hacking and snippet-borrowing from node-validator by Chris O’Hara, we can build another simple module called Tidy:
We can now improve our Greetings module, including the dependency and functionality:
var Tidy = require('./tidy');
var Greetings =function(){}
Greetings.prototype.hello=function(who){
who = Tidy.trim(who);
returnthis.hello(who);
}
module.exports= Greetings;
To see if this will work as expected, let’s add some spaces to the parameter passed in the NodeJS script:
var Greetings = require('./greetings');
var greetings =new Greetings();
console.log(greetings.hello(' Node ')+"!");
When we run it, this is what we see:
$ node runner.js
Hello Node!
Good, so this works on the server side, but let’s test its behaviour on the client side. Again, we should add some spaces to the parameter passed to ensure that it is working:
require is not defined
[Break On This Error] var greetings = new Greetings();
Our attempt to “require” the dependency in the Greetings module has broken the client side. To fix this, we need to define our own require() function, much like we had to for the module object. We do this by creating a new front end-only script, which we’ll call “middle-end.js”.
Now when we reload the page in the browser, all should work as expected. Huzzah!
This is just a very simple example of what can be done when JavaScript code is shared between the client and server sides—the “Middle End”. This concept becomes really exciting when you start thinking about potential approaches to form validation, session management, and even display logic. This is definitely something worth playing around with.
Note: This is the second in a series of posts describing my experiments with constructing a Middle End. In Part 1, we discussed the concept of the middle end and its advantages.
In my previous post, I talked about Kyle Simpson’s concept of a Middle End—a layer between the front end and back end. Today, I’m going to sketch out my very simple experiments with constructing such a layer.
What I am focusing on here is simply a means of sharing JavaScript code between the client side and the server side.
Let’s start with a very simple NodeJS Module, greetings.js:
var Greetings =function(){}
Greetings.prototype.hello=function(who){return"Hello "+who;
}
module.exports= Greetings;
Next, let’s create a very simple NodeJS script that imports this module, and uses it:
var Greetings = require('./greetings');
var greetings =new Greetings();
console.log(greetings.hello('Node')+"!");
When we run it, this is what we get:
$ node test.js
Hello Node!
All very simple stuff.
Now, suppose we want to use the Greetings module in our front end web app.
Back in October last year (2010) a bunch of us fled Berlin and made for Warsaw. Not just for the Vodka you understand, but more specifically for the Front-Trends conference.
One of the talks was discussing the concept of the Middle End, by Kyle Simpson. This really made an impression on me, and I think I probably bored Kyle in the bar afterwards with my drunken enthusing. When we arrived back home, my aim was to start playing around with the idea of a Middle End as soon as possible. Unfortunately, as is life, events took over (such as discovering one is going to become a dad!) and these thoughts were consigned to the back of my mind.
This is just a quick post as I’m off to bed for an early night’s sleep. JSConf EU is tomorrow and am one of the lucky few who have in their posession a “golden ticket” to attend. It’s a jam-packed event starting at an unaturally early 8am! Do they know this is Berlin?! You can’t even buy breakfast at 8am on a Saturday here!
I have high hopes for the conference. Last year I didn’t even attend the main event, just the pre-party on the Friday night and still came away with a load of ideas! One conversation lead me to completely rewrite major parts of the Jabbakam system. Who knows what new thoughts will spark in my head when I actually go to the real thing?
I’ve been hacking about with NodeJS a lot recently, and lately my attention has turned towards spawning child processes. Luckily NodeJS makes this very easy to do, and also the documentation is pretty good.
What I have been working on is having a single parent process that runs constantly in the background, and this parent is responsible for child processes which actually do the work. One thing I found particularly interesting was how the parent can handle a child crashing, and also what happens to the children when the parent is killed. Below is the culmination of my hackings, it comprises of two simple scripts, a child script, and a parent. I have committed them to my GitHub account, so feel free to clone, fork, hack or whatever.
As NodeJS v0.4.0 is out, I thought I’d have a play around with it (something I haven’t done in a very long time!)
Instantly I ran into problems:
$ ./configure
Traceback (most recent call last):
File "/Users/greim/nodestuff/node/tools/waf-light", line 157, in
import Scripting
File "/Users/greim/nodestuff/node/tools/wafadmin/Scripting.py", line 146
except Utils.WafError, e:
^
SyntaxError: invalid syntax
I’m trying to install this on my Arch Desktop, which has Python version 3.1.3 installed. After someresearch I found that it was a compatibility issue between Python 2.x and 3.x.
I didn’t really want to remove Python 3 as I’d had dependencyhellrecently. Inspired by an answer on StackOverflow, I had a dig about in my /usr/bin directory to see if there was both Python v3 and Python v2 installed. Fortunately there was: /usr/bin/python2
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.
The URL Module
The basis of all routing is the URL. Fortunately, Node has some basic functions which handle reading the URL so we can then decide what the user is actually trying to do.
First, we want to make sure we have included the URL module.
The aim of this tutorial is to create a Node powered web app using a CouchDB database and Haml 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 “top 10 list” type application. The functionality being as follows:
To list a number of items
To remove an item from the list
To add an item to the list
Obviously, before getting cracking with this tutorial I had to think up a name for this “killer app” of the Node world.
So I give you ErdNodeFlips! In honour of erdnussflips – the strangely addictive Wotsit type things that taste of peanut butter and stick to the top of your mouth. Yum! (OMG – they even have their own Facebook page!)