An Introduction to Bricks.js

Tuesday, October 4, 2011 - Jerry Sievert

It's been a few months since I released Bricks.js, and I figured it was finally time to talk about it. Bricks is a fast, and extremely modular web application framework built on top of Node.js that works a little differently.

Sure, there are routes but Bricks treats them a little differently -- think Apache Hooks combined with RegExp or truth-value based routing: complete flexibility for every route at every point in the routing process. In the case of Bricks there are four routing phases, or sections. Each of these queues get run sequentially, and any matching routes are executed until either the queue has been completely drained or response.end() is called. Once either of these occur the next routing phase is executed in turn until the fourth and final section has been completed.

The Sections

  • Pre (writeable) - redirects, aliases, sessions, permissions
  • Main (writeable) - main application logic, static files, 404 errors
  • Post (writeable) - html cleanup, header changes, compression
  • Final (read-only) - log files, analytics, final cleanup, user data

There are also events -- notifications when certain aspects of the routes are completed or for any sort of error during the execution. These allow for special or delayed processing; eg, when the final route is complete to save session data. They also allow for advanced stack traces to be sent to a developer when something goes wrong, as well as allow for the decision of a reasonable 500 recovery or response.

Bricks is also extremely modular. Pretty much any part of it can be replaced at any time, allowing for easy integration with socket.io, rewriting the routing engine to be more Sinatra compatible, or simple addition of instrumentation. It also comes with a handful of plugins to get your web application up and running as quickly as possible, as well as a static web server instance that can be run from the command line.

There is a lot more to Bricks than I can convey here, so you are encouraged to dive into the Bricks.js website as well as the source to learn everything that it has to offer.