Nodejs – First thoughts

My first thoughts on Node as a *real* beginner.

First Take.
I picked up Nodej about 3 years ago when the name sounded cool in the halls of Yahoo. I didnt think much of it and just thought it was the next buzz word. From a PHP developer’s stand point and mostly the standpoint of a MVC framework dude I can honestly say I didnt get it. Here’s what I didnt understand about Nodejs.

  1. No webserver like Apache, nginx, etc.
  2. Javascript in the Backend?
  3. Where’s the routing?
  4. I had to embed my HTML into the JS?
  5. I have to create all new components I have grown to rely on from scratch using Node?

Yep, I was wrong…

Node as a Web Server.
I’m used to Apache, Nginx, and to some degree lighttpd. It has build in modules I can use (mod_*), is super fast (nginx), and can even route my paths any which way (rewrite rules). So why do I need Node?

Because it’s all of those rolled into one. It has modules from NPM, is lightning fast (yet to benchmark), and has advanced routing using modules and/or a framework such as Express. Basically, I was just used to Apache, Nginx is what im saying. Let it go. :-p

Routing & the Uglyness that’s Javascript
OK! so here is where my n00b status comes in. I think Javascript is ugly enough to scare off its own mother. I will be the first to admit (and I know, not the last) that I can’t stand the sheer number of callback function you encounter in a JS file. Wow, blah. Based on what I’m seeing though, there’s a number of tools that allow you reduce the number of callbacks and abstract most of the underlining JS code by using a Framework or a MongoDB ODM such as Express and Mongoose.

Routing. So here’s where I was a bit lost. How did Node map or to a document in the file system. Apache, Nginx, map those paths to a document in the web root (or where your DocumentRoot is pointed to). So it’s a no brainer. If you’re using a framework you set up Routing Rules or the framework dictates how it will handle paths by default (think about ZF and Cake). With Node, it’s a different story. You have to create all of it. Instantiate a server, bind it to a port, create a router (or use the FileSystem ‘fs’ module), pass the router to the server, set the encoding type, listen for query data, listen for post data, attach methods to scrub data from query string, add….you get the idea. Ugh.

When starting off, yes this seems overwhelming but take a look at Express or another Node framework. It’s all taken care of.

View Separation
HTML in the JS? “Yea no thanks”, was my first reaction. I used to be a front-end developer back in the days and that was trying times. As I started using Node, I realized I had to do something like this…

response.write("SOME REALLY CRAZY HTML HERE");

This turned me off a bit so I kept digging for a View Renderer. Sure enough I found a few engineers using ‘fs’ to pull up a .html file which limited the functionality of the .html file since you couldn’t pass in variables much like a ZF Controller using $this->view->x = “hi there”. So I stumbled upon something nice, EJS. Take a look at that, it was exactly what I wanted!

NPM – Package Manager
One of the nice features of RoR is gem and in the PHP world, PEAR and PECL. Node has something like it but with less documentation (there’s talks on the irc channel for a better system) it’s called NPM or Node Package Manager. It’s very easy to install but you’ll need Node 0.6.X to properly run it. A simple

npm install mongoose

installs mongoose inside a node_modules directory in the path you ran the command. Very easy.

I like what I see. Though I have to admit I don’t yet understand completely the “event-driven” piece to it, I can see why it’s beneficial. I like how the pieces are coming together, and unlike Scala…it doesnt run on Java (see my Scala first thoughts post). So, yes you can say I’m sold.

Quick Helpful Links
#nodejs – IRC (freenode) – Download it and read the blog
Felix’s Node.js Guide – This guy has tons of neat links. I suggest you take a weekend and eat it up.
Free Beginner Node Book – Very nice beginners book to get you up and running. Recommend & Its FREE!

Add a Comment

Your email address will not be published. Required fields are marked *