Buttress is a supportive rapid prototyping library for NodeJS that provides both server and client helpers.

Buttress for Node.js

NOTE: Buttress is currently experimental -- it will change often. Use at your own risk!

Supportive rapid prototyping library for NodeJS that provides both server and client helpers.

$ npm install buttress

This little library is packed with goodness. But, that also means there are lots of snippets floating around, and that may be overwhelming.

All the code is meant to make your life easier in the long-run. It's not just a 5 line demo, but a real node.js application support structure with the details explicitly laid out.

You are free to use whatever bits a pieces of buttress that you want. You don't need to structure your code like the examples, but your life will probably be easier if you do.

This README assumes your project's assets are set up with the following folder structure:

|- app.js (main server-side node app)
|- protected (files that get compiled or compressed)
|   |- js
|   |  |- main.js (main client-side js)
|   |- style
|      |- index.less
|- public (public static files server from here)
    |- js
    |   |- main.js (minified version of protected js)
    |- style
    |   |- index.css (direct, or generated from less)
    |- img
        |- favicon.png

On the server, app.js will look something like this:

// A header so we know when the app is restarted. 
console.log("** Starting Node service **");
console.log(new Date());
var DEBUG = (process.env.NODE_ENV === "debug");
console.log("LAUNCH MODE -- "+(DEBUG?"DEBUG":"PROD"));
// Get the party started... 
var buttress  = require('buttress');
var atb       = buttress.atb()({}); // the AppToolBelt! 
var express   = atb.express;
var _         = atb.underscore;
var app       = express();
var http      = require("http");
var httpapp   = http.createServer(app);
// -- 
atb.enableHighAvailability(http); // drastically helps under load. 
atb.appDefaultRoutes(app, 200);   // cookies,bodyparser,gzip,503-on-overload. 
// -- 
app.get('/', function(reqres){
  res.send('Hello World');
// -- 
  // -- Auto: LESS -> CSS (only do this in the debug/dev branch) 
  var lessr = buttress.lessr();
                __dirname+'/public/style/index.css', true, { 
    paths         : [__dirname+"/protected/style"],    // .less file search paths 
    optimization  : 1,                // optimization level 1 is good (2 is unstable) 
    filename      : "index.less",     // root .less file 
    compress      : true,             // compress? 
    //yuicompress   : true,           // use YUI compressor?  
    autoMS        : 250               // how often to check watchfile for updates. 
  // -- Minify JS (from protected to public); once on app launch. 
  var reqjs = buttress.reqjs()({baseUrl: __dirname+"/protected"});
  reqjs.optimize("main", __dirname+"/protected/js", false, __dirname+"/public/js/main.js");
  // -- Pass through to protected JS (debug/dev branch only). 
  app.use('/js', express.static(__dirname+'/protected/js'));
app.use("/_lib", express.static(buttress.clientLibDir));
app.use(express.static(__dirname + '/public'));
var server = app.listen(process.env.PORT||80);
process.on('SIGTERM', function(){
  // do anything you want before the process is killed (shutdown gracefully). 
  return atb.onShutdown();

On the server-side, use the app toolbelt like so:

var atb = buttress.atb()({});

Here's what's inside atb:

  • atb.express -> passthrough to require("express").
  • atb.underscore -> passthrough to require("underscore").
  • atb.getClientIp(req) -> gets the IP from a request.
  • atb.getClientIpBase36(req) -> gets the IP as Base36 from a request.
  • atb.escapeHTML(txt) -> escapes characters that would mangle html output (&,<,>).
  • atb.bytesToMB(num) -> converts a number of bytes to a string representing MB.
  • atb.appDefaultRoutes(app,maxLag) -> a handy shorthand for common express routes such as: cookies, bodyparser, gzip, and 503-on-overload.
  • atb.enableHighAvailability(http) -> a posix util that drastically helps under load.
  • atb.useMemWatch(showStats,showHeap) -> log memory usage and alert when something seems leaky.
  • atb.onShutdown() -> should be called when your app is given the SIGTERM to end gracefully.

Buttress includes a handful of helpful javsacript modules and assets to make life easier. It also provides counter-parts to some of the server-side capabilities.

Providing access to the client-side files is simple via a pass-through in your node server app.js. Something like this is usually placed near the end of your code:

app.use("/_lib", express.static(buttress.clientLibDir));

This enables client-side webapps to see common assets and scripts in the ./_lib virtual folder.

Additionally, you'll probably want to provide a static file service for app-specific javascript, images, etc. in app.js like this:

app.use(express.static(__dirname + '/public'));

Ok, now on to the code that the client actually sees!

Note that client-side libraries are split into 3 parts:

  1. jscore: helpful js includes that don't depend on anything else.
  2. jsmods: requirejs modules.
  3. jsworkers: stand-alone webworkers for heavy tasks.

Buttress is designed to make the client-side html simple and scalable. Here's a template for a site's index.html as a starting point (with lots of common things you'll probably want, like app description, etc).

<!DOCTYPE html> 
<html lang="en-US" dir="ltr">
    <meta charset='utf-8' />
    <!-- title -->
    <meta property="og:site_name" content="APP_TITLE_HERE" />
    <meta name="apple-mobile-web-app-title" content="APP_TITLE_HERE" />
    <!-- desc -->
    <meta name="description"  content="APP_DESCRIPTION_HERE" id='pageDescription'>
    <meta property="og:title" content="APP_DESCRIPTION_HERE" id='ogTitle' />
    <!-- app icon -->
    <link rel='shortcut icon' type='image/png' href='/img/favicon.png'>
    <meta property="og:image" content="APP_URL_HERE/img/favicon.png" />
    <!-- viewport / mobile (no zooming) -->
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <!-- style -->
    <link href="/style/index.css" rel="stylesheet" type="text/css" />
    <!-- javascript from buttress via '_lib' -->
    <script src='/_lib/jscore/jquery.js'></script> 
    <script src='/_lib/jscore/underscore.js'></script> 
    <script src='/_lib/jscore/init_browser.js'></script> 
    <script src='/_lib/jscore/require.js'></script> 
    <!-- app code -->
      requirejs.config({baseUrl: '/', paths: {main: "js/main"}});

And js/main.js will look something like this:

// basic requireJS setup. library module and local project module loading... 
  var exports = {};
  var main    = exports;
  // -- 
  // make things accessible to the outside world  
  // by adding them to 'exports' 
  exports.exampleLinkifyTxt = function(txtToLinkify){
    return linkify(txtToLinkify);
  // --  
  function _onReady(){
    console.log("App is ready!");
  // -- 
  return exports; // return the exports. 

All new code by akumpf is licensed as open source under the MIT License.

The MIT License (MIT)
Copyright (c) 2013 Adam Kumpf
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

All other open source code is licensed as indicated and included simply out of convenience. You should really go check out their work directly. It's pretty awesome.

  • jQuery: https://github.com/jquery/jquery
  • underscore: https://github.com/jashkenas/underscore
  • d3: https://github.com/mbostock/d3
  • requirejs: https://github.com/jrburke/requirejs
  • less: https://github.com/less/less.js/
  • lame: http://lame.sourceforge.net/
  • tuna: https://github.com/Dinahmoe/tuna
  • sockjs: https://github.com/sockjs
  • filesaver: http://purl.eligrey.com/github/FileSaver.js
  • bbq: http://benalman.com/projects/jquery-bbq-plugin/
  • dsp: https://github.com/corbanbrook/dsp.js/
  • linkify: http://benalman.com/code/test/js-linkify/
  • markdown: https://github.com/evilstreak/markdown-js
  • add2Home: http://cubiq.org/add-to-home-screen

Special thanks to The Chaos Collective (http://chaoscollective.org) where a lot of this work started.