Getting started with Node

I’ve been building an application in Node lately, and I thought I’d share a couple things that will make getting started with Node far more pleasurable.

Exception tracking

It is very useful to know how to get more information on exceptions when you want it, and how to keep them from killing the server in production.

Object size

Sometimes you have a key / value map of objects stored by id for example. In Node, you get to say

var notes = { 3: { Note: { text: hi, ... } }, 17: { ... } };
log(Object.keys(notes).length);
2

Sweet

Backbone.js

Use Backbone. Just use it, from the very beginning. Try reusing models on the front end and backend. Or just sync the entire application state.

Express

If you’re building any sort of web app, not using Express is probably a waste of time.

Foreman and ReadyJs

These two npm power ups make managing your environments a breeze.

sudo npm install foreman
sudo npm install readyjs
vi Procfile

web: NODE_ENV=production node app.js
localhost: readyjs app/ jslibs/ --nocompiler; readyjs app/ jslibs/ --nocompiler --nojslint; readyjs jslibs/ www/js/ --order "underscore.js, backbone.js, jquery.jeditable.js, jquery.jtml.js, all.js" --nojslint --nocompiler; NODE_ENV=development node --debug app.js

What the localhost line is doing is running ready js once with jslint turned on, on my application files only. It runs it again without jslint, just in case it doesn’t pass, so we’re sure the compiled file is where it should be. Then it concats all the libraries used, in order needed for loading, and we’re good to go. Now all you need to type to launch your app is

forman start localhost

To make this even better, add a Git hook, so when you deploy everything is how you want it for production straight from the repo.

Heroku

Offers free development hosting, and integrates with Foreman, super easy deployment. Lot’s of cool free add-ons as well to add some spice. Definitely worth a look.

Hopefully these tips will save you some time, feel free to comment with any tips you’ve found as well.

Leave a Reply

Your email address will not be published. Required fields are marked *