In my previous post I mentioned that I needed to come up to speed with CoffeeScript for my next project. That same project will be using Node on the server side. Since I liked the CoffeeScript book I thought I would give Node for Front-End Developers a read. Once again, O’Reilly has published a book that is a quick read but gives you enough information so that you can put the book down and start writing code. I found this book gave me enough information to start building my first node based application.
The book starts off by walking the reader through how to set up his/her node environment. It walks you through installing node and its package management system, npm. Once you have your environment set up the you are introduced to the REPL (read – evaluate – print – loop) with a few short code snippets. The chapter ends with a discussion how to declare which modules your application depends on by using the package.json file.
Chapter two walks the reader down the path of serving up resources be it a string of HTML or static resources like HTML, CSS, and/or JS files. The first example shows you how to write a server that serves up HTML ‘by hand’ which is then quickly followed by how much easier it is to serve static pages when you use npm modules like connect.
After learning how to serve static files the author introduces shows you how to interact with the client by processing HTTP GET requests. The first example uses the module querystring to process GETs with parameters. It reminded me of the early days of the web. Thankfully after another example of processing parameters on the URL with querystring, the author shows you how to do the same thing in a more concise manner using the connect module.
The next topic was how to process HTTP POST requests. We followed the same pattern here, learn how to ‘roll your own’ to process a POST which was then followed by an example of how to do it using the connect module. I liked the approach the author takes throughout the book, show you how to do it yourself first and then introduce a module that can do the same thing in a less verbose manner.
After a brief overview of how to handle JSONP requests the discussion moved onto real-time communication using socket.io. Having just finished a project that uses SignalR (which is a .NET open source project that has similar functionality) I found this subject very interesting. The example made socket.io seem simply and straight forward. In fact, after I finished reading this chapter, I started a spike to redo the project I just finished using socket.io just to see if it was any easier with socket.io.
Chapter four introduced the type of server side templates. It walks you through how to use mustache to layout templates for your application showing you how to use templates and sub templates to promote re-use on the UI side. Towards the end of the chapter the author starts to discuss best practices on how to group your code, separate out code that handles a certain task to promote code re-use and separation of concern.
The next topic of discussion was data access and application flow. The first part of the chapter uses Redis to show the reader how to work in data access to your node applications. I hadn’t worked with Redis before but after that part of the book I am now looking into incorporating it into a few projects currently underway. After the Redis discussion was complete workflow was discussed using a pub/sub example using events.
Up to this point in the book I found the flow of each chapter easy to follow. I appreciated the process of doing it by hand and then doing it again with a pre-existing module. It’s a great way to show you how something works and exposing you to the node module ecosystem. It seems like there’s a module for just about anything you may want to do.
The last chapter varied from the previous ones by jumping into a big chunk of code right away. The code is an example of how to create an MVC application with node. On the plus side, this chapter introduces the express module, which was inspired by ruby’s Sinatra web framework and is used by many other node modules. Express has a tool that will create a directory structure for your app and has a view engine, Jade, to create HTML views. Jade’s syntax takes a little bit to get use to but once you do it makes creating HTML views easier and much easier to read. Overall the chapter wasn’t bad it was just a little code heavy when compared to the previous chapters.
I came to this book with very little experience with Node.js, I had created a small app for my personal use but nothing huge. Now that I have read this book I feel comfortable enough to use it in a few projects I have on the horizon. I liked the methodology the author took for the first five chapters of doing it the hard way and then showing the reader the easier way to do with available modules.
On a side note, this is the second short book I’ve read by O’Reilly. I hope they continue these type of short but sweet introductions to new(er) technologies. It certainly helps us come up to speed quickly.