diretto

Filesystem-based routing for Express.js

Diretto

A file-based router for Express / Connect

Diretto makes it easy to create content folders (can be named anything) from to easily serve content with granular control of application behaviour. It's inspired by files-based CMSs like Stacey, but it uses Node.js and Express.js.

Create a folder in you project, anywhere (project root in this example):

/appDir
    /projects
        /project-1
            /public
                style.less
            index.jade
            index.yml
        /some-other-project
            /public
                style.less
            index.jade
            index.yml
        /...

The YML may look like this (url is required):

title: Coke Beatbox
description: This is a description for the Google
url: some-cool-project
id:
author: Tommaso <t@theworkers.net>
date:
  modified: 2013-03-13
  published: 2013-02-23
published: true

Then you can use it like this:

// Require Diretto 
var diretto = require('connect-diretto');
 
// Create a new instance pointing to the content folder 
var projects = diretto(__dirname + '/projects');
 
// Mount the middleware 
// Note we mount the middleware to respond on /work (this could be 
// anything or left empty). Watch out for middleware stack order! 
app.use('/work', projects.app);
 
// Set it up 
projects.each(function(appconfig) {
    app.use(express.static(path.join(config.__dirname, config.__filename, '/public')));
    app.set('views', path.join(config.__dirname, config.__filename));
    app.use(lessMW({ src: path.join(config.__dirname, config.__filename, '/public') }));
    app.use(app.router);
});
 
// Add routing 
// '/' will in fact respond to '/work/some-cool-project' 
// Diretto adds .diretto to the request object. 
// Config is a shorthand to the currently matched config 
projects.each(function(appconfig) {
    app.get('/', function (reqresid) {
        res.render('index.jade', _.defaults(req.diretto.config, {
            extraData: 'extra'
          , moreExtra: 'encore'
        }));
    });
});