Node.js

This week it is Handlebars. The JavaScript templating library. After starting out with Express JS for Node, it defaults to Jade templating language. Whilst I’m sure its an amazing language will lots of bells and whistles, it is quite different from normal HTML. It requires you change all of the HTML into its format.
I’d prefer to stick with normal HTML, to keep it similar to other languages I work with (PHP) and easier to use frameworks such as Bootstrap.

I had a quick look around for an easy to use templating language that would play nicely with Express, and be easy to use, from someone coming from PHP / Smarty background.
I found Handlebars, which allows you to print variables with a double curly bracket {{var}}. Nice and easy.
The real reason I chose it was of course, because it is currently Movember and Handlebars whilst already being a style, is derived from the moustache templating language. Handlebars adds features to Moustache and has a polished and easy to use Node.js modules. I personally chose ‘Express3 Handlebars’ installable with ‘npm install express3-handlebars’. I tried another but this one worked perfectly first time.

This weeks code of the week is Socket.io a easy to use and nicely packaged web sockets framework. Installing it with npm can’t be easier, and it works nicely with express too.

Best features are the easy to use listeners, broadcasting to just the client, everyone but the client or everyone. Which all come in very useful. It falls back to flash for old browsers seems stable and lots of documentation available online.

I built my cake off count down using Socket.io to broadcast users mouse movements to each other, so you can see their little spaceships whizzing around. I will open source the code to it soon, but for now I most worry about going to the airport for 4am.

I decided to get Node.js running on the same server as Apache. I wanted to keep Apache exactly the same, so not messing with .htaccess files and to keep it manageable with Virtualmin. I also wanted to keep using the same server as it had plenty of spare capacity.

Currently the server is a normal LAMP stack as shown by the diagram below.

LAMP stack

The solution was to get another IP address; get Apache to use one, use Nginx on the other. I decided to use Nginx as its apparently faster at serving static files than Node and will allow other services to be proxied to in the future if needs be. It also looks to be safer as a recent DoS vulnerability in Node is not achievable when Nginx is proxying.

Apache Nginx and Node.js on the same server

I also decided to try out RethinkDB as a Nosql database for Node.js, it has officially supported drivers for Node and stores data in JSON. I could have used the more popular MongoDB but Rethink’s admin interface caught my attention. Node can also connect to the same MySQL server as PHP currently does. This gives the potential to allow Node to do background processing for PHP sites.

I’ll do another post in the near future detailing how I got it working, including a few bumps along the way.