flatiron /director

a tiny and isomorphic URL router for JavaScript

★ 5,568⑂ 475👁 155⑃ 133● 228▤ 0
JavaScriptMITCreated 2010-04-24github.com/flatiron/director
+0
Daily
5,568
stars
475
forks
155
watchers
133
prs
228
issues
0
releases

README

!Logo

 Synopsis

Director is a router. Routing is the process of determining what code to run
when a URL is requested.

 Motivation

A routing library that works in both the browser and node.js environments with
as few differences as possible. Simplifies the development of Single Page Apps
and Node.js applications. Dependency free (doesn't require jQuery or Express,
etc).

 Status


 Features

 Client-Side Routing
 Server-Side HTTP Routing
 Server-Side CLI Routing

 Usage

 API Documentation
 Frequently Asked Questions

 Building client-side script

Run the provided CLI script.

bash
./bin/build


 Client-side Routing

It simply watches the hash of the URL to determine what to do, for example:


http://foo.com//bar


Client-side routing (aka hash-routing) allows you to specify some information
about the state of the application using the URL. So that when the user visits
a specific URL, the application can be transformed accordingly.

!Hash route

Here is a simple example:

html


  
    
    A Gentle Introduction

    
    

    
      var author = function () { console.log("author"); };
      var books = function () { console.log("books"); };
      var viewBook = function (bookId) {
        console.log("viewBook: bookId is populated: " + bookId);
      };

      var routes = {
        '/author': author,
        '/books': [books, function() {
          console.log("An inline route handler.");
        }],
        '/books/view/:bookId': viewBook
      };

      var router = Router(routes);

      router.init();
    
  

  
    
      /author
      /books
      /books/view/1
    
  



Director works great with your favorite DOM library, such as jQuery.

html


  
    
    A Gentle Introduction 2

    
    

    
    

    
    $('document').ready(function() {
      //
      // create some functions to be executed when
      // the correct route is issued by the user.
      //
      var showAuthorInfo = function () { console.log("showAuthorInfo"); };
      var listBooks = function () { console.log("listBooks"); };

      var allroutes = function() {
        var route = window.location.hash.slice(2);
        var sections = $('section');
        var section;

        section = sections.filter('[data-route=' + route + ']');

        if (section.length) {
          sections.hide(250);
          section.show(250);
        }
      };

      //
      // define the routing table.
      //
      var routes = {
        '/author': showAuthorInfo,
        '/books': listBooks
      };

      //
      // instantiate the router.
      //
      var router = Router(routes);

      //
      // a global configuration setting.
      //
      router.configure({
        on: allroutes
      });

      router.init();
    });
    
  

  
    Author Name
    Book1, Book2, Book3
    
      /author
      /books
    
  



You can find a browser-specific build of director [here][1] which has all of
the server code stripped away.

 Server-Side HTTP Routing

Director handles ro

Top issues

Snapshot: 2026-11-16T09:23:14Z · Data license Apache-2.0 · JSON