Building Applications With

Bricks.js


git://github.com/JerrySievert/nodepdx-bricksjs.git

What is Bricks.js?

Routes

Multiple routes (think Apache hooks)



    #define 	APR_HOOK_REALLY_FIRST   (-10)
    #define 	APR_HOOK_FIRST          0
    #define 	APR_HOOK_MIDDLE         10
    #define 	APR_HOOK_LAST           20
    #define 	APR_HOOK_REALLY_LAST    30
      

Routes in Action



    // pre - writeable
    appServer.addRoute(".+", star, { section: "pre" });


    // main - writeable
    appServer.addRoute("/api/(.+)", api, { section: "main" });


    // post - writeable
    appServer.addRoute(".+\.(js|css)", compress, { section: "post" });


    // final - housekeeping
    appServer.addRoute(".+", log, { section: "final" });
      

Matching is Easy

Flow Control

    function hello (request, response, options) {
      response.write("Hello world!");

      // finished responding
      response.end();
    }
      

Built-in Plugins

Plugins in Action



    // add request to any /wiki/ routes
    appServer.addRoute(
      "^/wiki/.+",
      appServer.plugins.request,
      { section: "pre" }
    );

    // add the wiki routes, match for /edit first
    appServer.addRoute("^/wiki/(.+)/edit$", wiki.edit);
    appServer.addRoute("^/wiki/(.+)$", wiki);

    // static files
    appServer.addRoute(
      ".+",
      appServer.plugins.filehandler,
      { basedir: "./htdocs" }
    );

    // anything else gets 404'd
    appServer.addRoute(".+", appServer.plugins.fourohfour);

    // log the results
    appServer.addRoute(
      ".+",
      appServer.plugins.loghandler,
      { section: "final", filename: "access.log" }
    );
      

Anatomy of a Plugin

What makes it different than a function()?



    // executed at route creation
    exports.init = function init (options) {
      console.log("inside init");
      console.dir(options);
    };


    // executed for a route match
    exports.plugin = function plugin (request, response, options) {
      console.log("in plugin");
      console.dir(options);
      
      response.next();
    };

    
    // meta data
    exports.meta = {
        name:        'sample plugin',
        description: 'An awesome plugin that does sample like things'
    };
      

Getting Bricks.js

Let's Create a Wiki!

Me