node package manager


Grand Central Junction

A simple Rails-inspired router for Node built on top of Express.


route(app, options)

Options that can be specified:

  • dir -- the home directory of the app. Defaults to the directory that contains the node_modules folder
  • routes -- the routes path, relative to the directory route() is called from. Defaults to config/routes
  • controllers -- the controllers path. Defaults to controllers

In your app.js file or wherever you initialize your Express app, add the following code:

var express = require('express'),
    gcj = require('grand-central-junction'),
    app = express();

With custom options:

gcj.route(app, {
    dir: __dirname + '/app',
    routes: 'router/routes',    // Will get the routes from ./app/router/routes.js 
    controllers: 'controllers'  // Will look for controllers in ./app/controllers/ 


Example of /config/routes.js:

var oauth = require('../oauth');
match('/',         'home#index');
match('/user/:id', 'user#get');
match('/user',     'user#create', {via: 'post'});
put('/user/:id',   'user#update');
del('/user/:id',   oauth.requireAuth, 'user#remove');
resources('/animal', 'animal');


GET    /            => /controllers/home.js#index
GET    /user/:id    => /controllers/user.js#get
POST   /user        => /controllers/user.js#create
PUT    /user/:id    => /controllers/user.js#update
DELETE /user/:id    => /oauth.js#requireAuth >> /controllers/user.js#remove
GET    /animal      => /controllers/animal.js#index
GET    /animal/:id  => /controllers/animal.js#show
POST   /animal      => /controllers/animal.js#create
PUT    /animal/:id  => /controllers/animal.js#update
DELETE /animal/:id  => /controllers/animal.js#destroy
GET    /animal/create     => /controllers/animal.js#create
GET    /animal/edit/:id   => /controllers/animal.js#update
GET    /animal/delete/:id => /controllers/animal.js#destroy

match(route, action, [options])

A GET route to the specified controller/action.

  • route string regex-- the Express routing string/RegEx
  • action string function -- either a direct callback with req, res params or a string in the format of 'controller#action', with controller being the name of the file in the controllers directory and action being the name of the method in the controller.
  • [options] object
    • via changes the method of the route: get (default), post, put, delete
    • method same as via

VERB(route, [action...], action)

Same as match() options above, but without options and the ability to include many actions. VERB = get|post|put|del|all

resources(route, [controller])

Maps all HTTP methods to their respective CRUD actions in a controller. The action names are the same as those in Rails with a few extras added in (see above list of routes for names).

  • route string regex -- the Express routing string/RegEx
  • controller string -- the name of the file in the controllers directory, so 'project' would be the name of ./controllers/project.js. This defaults to whatever name is in the route string.