I helped Clean Slate DC add Google Analytics to their app last night at Code For DC and realized others might not know how to do this. It’s pretty simple, but you won’t want to follow the directions on Google Analytics exactly. Instead, add this to your index.html or main page <head>, replacing your UA code with the UA code that Google gives you:

SyntaxHighlighter Evolved is currently the plugin I use for syntax highlighting on this site. It uses the default text size for code blocks, which doesn’t look great since code is in a fixed-width font. To fix that, add a class to the code blocks called code-blk in Settings > SyntaxHighlighter. Then modify your theme’s CSS (or use the WordPress Jetpack Custom CSS feature) to add CSS like this:

Overall, I’ve had a good experience with HapiJS and Code, Hapi’s assertion library. There was one gotcha with Code’s deep.equal function. If you are comparing two objects that have identical non-function properties, but different function properties, deep.equal will fail. So, for example, if you compare a Hapi response to a Plain Old Javascript Object fixture, you will get an exception.

The solution is to strip the non-function properties before comparing:

This post is an work in progress list of tips and tricks I’ve learned working on a HapiJS API after mostly coding Rails APIs for the last few years. I expect to update it as I learn more.

If you are a Rails developer looking to transition to NodeJS, hopefully this will save you some time. If you are a more seasoned NodeJS or Hapi developer and have suggestions for improving this, please let me know in the comments.

Async Programming

Error-first callbacks, promises, or async/await

Most Rails developers have written some Javascript, so you are probably familiar with callbacks. On the client, having nested callbacks is relatively rare. On the server, however, they are far more common. A simple example would be a server method that needs to make a handful of database calls, with all of the depending on the outcome of the others. This would be trivial to write in a sequential way, but with NodeJS, you will need to nest callbacks to accomplish the same thing.

This is both the best thing about NodeJS and the most annoying. It makes things very performant, because with callbacks you continually return control to the main event loop, freeing the main thread to move on to other things. In Rails, unless you are using a threaded server like Puma, waiting on a database call will block the entire process.

All of Node core and most of the libraries use error-first callbacks. If the first argument to the callback function is truthy, then you know the callback has failed with an error. You’ll see a lot of code in NodeJS like this:

This doesn’t look Earth-shattering, but promises can make deeply nested callbacks more readable. I like promises from my AngularJS programming, but they aren’t widely used in the Node community. If you attempt to use them, you feel like you are fighting an uphill battle since everyone still uses error-first callbacks. They can be vital in some situations, however.

ES7 and async/await

The next generation of Javascript will hopefully solve all of our problems with the addition of two new keywords: async and await. These use promises under the hood, but you can use try–catch and don’t need then():

You can use this today if you transpile with the Traceur compiler, but not much of the Node community is doing that yet. Once more people get on board, this will be a huge improvement in Javascript.

What do do today?

Today, I think you should stick with error-first callbacks and use promises if absolutely necessary. Another library you can look into is the async library, which gives you lots of nice functions to handle many callbacks in series or parallel.

Test folder setup

Running your specs

To run your specs, you have two choices. You can either install lab globally and run them with the lab command, or you can change your package.json file to set what to run for npm test. I usually do the second options, because it lets me specify some default options for lab. Here’s what the section of package.json file looks like:

{
"scripts": {
"test": "env $(cat .env | xargs) lab -v -L -m 0"
}
}

The xargs part is to set the environmental variables before running the tests from the .env file. There probably is a better way to do this, but that is working for now.

ETL – not the most fun to write, but the datapipes package makes it pretty easy on NodeJS.

One thing that wasn’t explained in the documentation well was how to ETL from one MongoDB to another using the MongodbMixin. The problem when you use the MongodbMixin twice in the same pipe is that it gets confused about which connection to use for which things. Here’s how to use pipe groups to fix that.

This error can show up when you are trying to load a schema into a new database in Postgres that has been dumped from a database with the pg_trgm extension. You can fix it by adding the extension to the template database (template1) so that when you create a new database, it is already there, ready to go.