Having played around a little with Node before in hack projects at work, I wanted to have a proper dive and get my hands a bit dirtier. As a result, I’m midway through another increment of http://lastband.in, this time with a node backend. Here’s some things I’ve discovered so far
Use Express 3.0 and persist with it
I started out wanting to do a slightly different project connecting facebook to some other bits and pieces. When you sign up with Facebook it offers to kick you off with a basic project in Heroku, which is a very nice feature; If you don’t know Heroku, it’s a great place to deploy stuff for free when starting out and offers a lot of nice tools.
I wanted to use handlebars as my templating library which should be easy enough, but I instantly ran against a ton of issues, mostly because I was ignorant to the ways of express; When Express went from Express 2 to 3 a lot changed, and it seems a lot of the internet hasn’t caught up yet. All of the mixed documentation got me in a faff so I switched over to use connect. DO NOT DO THIS! It’s very very difficult to google, there’s very little documentation on the main site and the internet has very little to offer.
On the other hand, express is as close to a standard as you can get in node land, and once you wrap your head around it and find some reliable articles, it’s actually quite usable!
How to use Express 3.0 with Handlebars
I was going to post the server up here but instead I’ve whacked it up on my github at https://github.com/samberic/express-handlesbars-starter.
Node.js is still very immature
Whilst node is undeniably awesome, very easy to use, and just a lot better for quick projects and fast progress, it’s still at the very early stages. A number of the projects I tried using in npm just wouldn’t work at all. Want to profile your app on a mac? Not a chance. All very frustrating, and would certainly be a blocker from using it in the enterprise.
You’re not in the browser any more
For lastband, it worked in 3 tiers; first, find the band on last.fm, render a handlebar template. Then, find artists with that name on bandcamp, render a handlebar template. Then, for each artist, get their discography, and render a handlebar template, then add them all together with a bit of jquery.
This was the easiest way to do it in the browser (I thought at the time), as I was dealing with 3 different API calls. When porting to node I spent ages looking at JQuery replacement libraries (most of which are based on jsdom and are terrible/won’t compile; If you’re going down this route I recommend Cheerio) at building the html up in this incremental fashion.
I got this working, but it was slow, the code was fugly, and it just didn’t feel right. As a result, I switched it up; after playing with JS for a bit I realised I could just append each API JSON result to the previous one, resulting in one massive JSON doc; Combine that with 1 single handlebars template, and voila! Now my template is clean and very obvious what’s going on, and my code is to.