flatiron 
An elegant blend of convention and configuration for building apps in Node.js and the browser
Example HTTP Server:
Example HTTPS Server:
Example CLI Application:
Run It:
Installation
Installing NPM (Node Package Manager)
Installing Flatiron
Installing Union (Required for flatiron.plugins.http
)
Usage:
Start With flatiron.app
:
flatiron.app
is a broadway injection container. To be brief, what it does is allow plugins to modify the app
object directly:
Virtually all additional functionality in flatiron comes from broadway plugins, such as flatiron.plugins.http
and flatiron.plugins.cli
.
app.config
flatiron.app
comes with a config
plugin pre-loaded, which adds configuration management courtesy nconf. app.config
has the same api as the nconf
object.
app.log
flatiron.app
will also load a log
plugin during the init phase, which attaches a winston container to app.log
. This logger is configured by combining the app.options.log
property with the configuration retrieved from app.config.get('log')
.
Create An HTTP Server with flatiron.plugins.http(options)
:
This plugin adds http serving functionality to your flatiron app by attaching the following properties and methods:
Define Routes with app.router
:
This is a director router configured to route http requests after the middlewares in app.http.before
are applied. Example routes include:
app.router
can also route against regular expressions and more! To learn more about director's advanced functionality, visit director's project page.
Access The Server with app.server
:
This is a union middleware kernel.
Modify the Server Options with app.http
:
This object contains options that are passed to the union server, including app.http.before
, app.http.after
and app.http.headers
.
These properties may be set by passing them through as options:
You can read more about these options on the union project page.
Start The Server with app.start(<host>, port, <callback(err)>)
This method will both call app.init
(which will call any asynchronous initialization steps on loaded plugins) and start the http server with the given arguments. For example, the following will start your flatiron http server on port 8080:
Create a CLI Application with flatiron.plugins.cli(options)
This plugin turns your app into a cli application framework. For example, [jitsu] (https://github.com/nodejitsu/jitsu) uses flatiron and the cli plugin.
Valid options include:
Add lazy-loaded CLI commands with options.dir
and app.commands
:
Flatiron CLI will automatically lazy-load modules defining commands in the directory specified by options.dir
. For example:
In the command, you expose a function of arguments and a callback. this
is set to app
, and the routing is taken care of automatically.
Here it is in action:
You can also define these commands by adding them directly to app.commands
yourself:
Callback will always be the last argument provided to a function assigned to command
Define Ad-Hoc Commands With app.cmd(path, handler)
:
This adds the cli routing path path
to the app's CLI router, using the director route handler handler
, aliasing app.router.on
. cmd
routes are defined the same way as http routes, except that it uses
(a space) for a delimiter instead of /
.
For example:
When you run this program correctly, it will say hello:
If not, you get a friendly usage message:
Check CLI Arguments with app.argv
:
Once your app is started, app.argv
will contain the optimist-parsed argv options hash, ready to go!
Here's an example:
This prints:
Awesome!
Add a Default Help Command with options.usage
:
When attaching the CLI plugin, just specify options.usage to get a friendly default message for when there aren't any matching routes:
Start The Application with app.start(callback)
:
As seen in these examples, starting your app is as easy as app.start
! this method takes a callback, which is called when an app.command
completes. Here's a complete example demonstrating this behavior and how it integrates with options.usage
:
Here's how our app behaves:
Read More About Flatiron!
Articles
Sub-Projects
Tests
Tests are written in vows: