30.6.15

90 days of node.js - Days 11 and 12 - Domains, Jade and Oauth

In case you don't know what this is all about, the out-of-loop link describing my 90 day journey through node land.

90 sequential days looks more and more of a pipe dream. What with work, family, books, podcasts and mowing the lawn, I've had quite a bit of a break. Days 11 and 12 ( yay, 10+ days) were more of a slog than any fun. I did pick up two interesting technologies though that I intend to use a lot more (hopefully professionally). I also find that blogging on a daily basis is down right hard, especially since I still want to write about books when I get a chance.

Let's talk days 11 and 12.

Jade isn't strictly node.js. It is a powerful templating technology. Over the years, I 've used a mish-mash of templating technologies - jsp, gsp, underscore and freemarker. Given all that, jade was a whiff of fresh air. Here's a sample from my checked in code:

h1 Showing Public lists of #{screenName}
div
    a(href="/twitter-authorize")Signin with Twitter
div.row
    div#nav-header.col-md-2
        span Lists
    div#members-header.col-md-2
        span Members
    div#subscribers-header.col-md-2
        span Subscribers
    div#tweets-header.col-md-2
        span Tweets
The output is pretty obvious - a forest of divs :). The great feature though are simple things. The markup is nice and indented, the ids and classes are applied with the least of ceremony and all html element attributes are treated as object properties. You can even inline javascript and declare custom tags (which are called mixins, but you know what they are). I'm really groking the simplicity this brings to the layout. Granted integrating it with something like bootstrap can be a bit of a pain, especially since the list of classes applied to elements can get too long, but I'm guessing the mixins will ease some of the pain.

The cool node thing that I stumbled into though was domains. According to the excellent node.js docs:

Domains provide a way to handle multiple different IO operations as a single group

Till my server started dying on errors, I didn't realise that the node processes kill themselves on uncaught errors. Listening to uncaughtException events leads to loss of context. Creating a domain and having it run the specific process can easily allow us to listen to errors and not fail miserably on errors.

I was trying to do some exception handling in my express server. Thanks to my choice in using promises and throwing explicit Errors when handling async calls, I was stuck with having to add a way to handle uncaught exceptions thrown by the async process.

Enter, connect-domain. It's a middleware which slots into the express middleware paradigm and allows a way to pass on the error all the way to a designated error handler. The sample code indicates how easy it is to use and I was able to start doing decent exception handling in the server. There is also a express-domain-middleware module that seems handy. I opted to ignore it for now, primarily because I am feeling an overdose of investigating modules. Unlike the javaland frameworks where things come bundled, having to install one module at a time is both time consuming and hard to sustain when beginning. There is documentation to read, figure out what the dependencies are and hack through a lot of code before "things just work"™

Which brings me to the final useful modules that I used. OAuth has been a most handy module. I tried to implement the OAuth provider on my own for Twitter login - probably not the smartest idea of all time. I killed a lot of cycles understanding and reading about oauth. By itself, reading and learning about oauth is a win, but taking into account the fact that I am not trying to build a simple twitter app, seemed overkill. There is also passport - the types of login it allows you to do is mind boggling. But again, I didn't need the wide range of login support that passport provides. So I reinvented a spoke of the big wheel. Definitely use passport for production apps. (unless you hate all the dependencies)

I find that my choices for technologies are highly colored by my experiences writing Java web applications (spring mvc or grails). Instead of CLI, which seems to be node's best experience, I opted for express since that is closer to my niche. Jade and templates are kin to jsps, so that flows well. Middleware makes perfect sense when you think of how many aspects and filters are involved in writing web apps. The next things in a Java app would have been wiring a real database and writing some tests. I still have to write some jasmine tests, so that will probably be my next step. After that I will probably dabble in a little mongo, go build that app using cli and do some documentation.

Fun stuff.