Sunday, April 15, 2012

Node.js After Hello World

Here are some useful things to know when you start developing web apps with node.js.

Developing with Eclipse

You can download a version of Eclipse for JavaScript Web Developers

If you already have a version of Eclipse installed for Java or some other type of development, it's recommended that you download this version separately as opposed to adding the JavaScript development plug-in to your current installation. Unlike some IDEs, developers are intended to have multiple installations of Eclipse on a single machine where each one is tailored to a type of development.

Debugging with Eclipse

Install Google Chrome Developer Tools plug-in for Eclipse. See the setup instructions on the Joyent github wiki.

Launch a node server in debug mode

node --debug your-app.js

Launch a node server that waits at the main script for the debugger to attach (for debugging things that happen during the initialization process too quickly to attach the debugger)

node --debug-brk your-app.js

It's annoying that you have to put break points in the files of the 'node debug' project instead of the original files in your own Eclipse project. And the breakpoints in the node debug project do not persist across debug sessions (arg!) If there is a way to do this, please let me know.

Node package manager - npm

The node package manager is called npm and is available at npmjs.org. Follow the instructions there to download and install.

Modules, Middleware, and Frameworks

The plain install of node offers low-level libraries for working with http and file i/o. There are a number of modules you can install to provide common functionality for a web application. These range from simple things such as serving files over http and compressing http responses, to robust MVC frameworks similar to Rails.

The Joyent wiki has a list of node modules and the npm site hosts a searchable registry of node modules.

When building a web app with node.js, developers decide which modules to use based on the usual dependency trade-offs of time saved writing code vs. time spent working with third-party code. I chose to start with Express because it offered extremely common features, but is less restrictive than the frameworks built on top of it.

Here is a simple list that shows where Express sits in a stack:

  • [Many web-app/mvc/rails-like frameworks]
  • Express - basic web-app features such as request routing, view rendering, and content negotiation
  • Connect - middleware for features such as logging, compression, and serving files
  • Node - low-level network and file i/o

Template engines

I'm using the Jade template engine. Template engines allow developers to mix application code with static mark-up and often have a way of abbreviating HTML syntax, e.g. Haml. Here's a list of some other templating options with reasons why I chose Jade:

  • Haml - I like Haml and Jade is intended to be an improved Haml specifically for node.js
  • EJS (Embedded JavaScript) - Uses "bumble bee" tags, e.g. Welcome to <%= place %>
  • CoffeeKup - CoffeeScript based

Auto-reload

Manually restarting node.js to reload code changes during development is a pain. Use node-supervisor to auto-restart the node process if it crashes or when files have changed.

[sudo] npm install node-supervisor

supervisor --watch [comma sep dirs] [your-app.js]
supervisor --watch .,routes,app/controllers,app/models app.js

nodemon is an auto-reload alternative to node-supervisor.