Guidance
A Rails like middleware for routes loading on expressjs applications. (heavily inspired from http://guides.rubyonrails.org/routing.html)
Purposes & use
This module provide a Rails like approach for defining routes.
It also add helpers for the views appending it to the res.locals
object.
Required configuration:
First of all define your controllers.
They are simple exported objects where the filename is the controller name, object keys are the controller's actions, and the object values are the route handlers.
Default location of the controllers is process.cwd() + '/controllers'
// controllers/welcomeController.js moduleexports = { return res; } { return res; };
Routes should be defined as a module that exports a function accepting the router object as parameter. Use the router object to define your routes.
Define your routes like this (usually in a separated routes.js file):
// routes.jsmodule { router; router;};
This snippets indicate that on GET /
the express app respond with welcome controller's index action, and GET /about
is mapped to the welcome controller's about action.
Finally use the middleware like this:
const express = ;const guidance = ;const routes = ; // the module defined above (routes.js) const app = ;app; ...
Naming conventions
Some conventions are adopted for the correct mapping of the routes.
controllers
When referring to a controller name, the controller file is resolved to <controllersPath>/<controllerName>.js
where:
- controllersPath is default to
process.cwd() + '/controllers'
(overridable withguidance.initialize
options). - controllerName resolves to the controller name without an eventual
Controller
suffix (welcomeController and welcome both becomes welcome)
actions
When referring to an action name, the action handler is mapped to the relative controller's property with the same name.
An eventual Action
suffix is removed from the name (indexAction and index both becomes index)
The controller's property should be obviously a callable.
Reference
Guidance
guidance.initialize(routes, options)
guidance middleware.
Accepted parameters:
- routes the defined routes module
- options.controllersDir the absolute path where to locate the controller modules. Defaults to
process.cwd() + '/controllers'
Router
Basic routing
Connect URLs to code in the following way (using the to
key)
routerrouter
When the express app receive a GET /home
request, the index action handler of the welcome controller is used; the express app responds also on POST /login
with the session controller's create action.
Any express supported method (router.METHOD) can be used.
Root
You can specify how to route GET /
with the root method:
routerrouter // shortcut for the above
Named parameters
Named parameters are also supported:
router
the id
parameter is available to the req.params
object of the action.
Named routes (views helpers)
A name can be assigned to the route (using the as
key):
router;router
This helpers are available to the views:
// returns '/hp' // returns '/patients/42'
Resources
Resources can also be defined:
router
This statement creates the following routes:
GET /photos
to photos#indexGET /photos/new
to photos#newPOST /photos
to photos#createGET /photos/:id
to photos#show with id as parameterGET /photos/:id/edit
to _photos#edit with id as parameterPATCH /photos/:id
to _photos#update with id as parameterPUT /photos/:id
to _photos#update with id as parameterDELETE /photos/:id
to _photos#delete with id as parameter
It also creates the following helpers:
photosPath()
returns/photos
newPhotoPath()
returns/photos/new
editPhotoPath(42)
returns/photos/42/edit
photoPath(42)
returns/photos/42
Multiple resources can be defined at the same time:
router;
Single resource
Single resource can be defined:
router;
This statement creates the following routes:
GET /geocoder/new
to geocoder#newPOST /geocoder
to photos#createGET /geocoder
to geocoder#showGET /geocoder/edit
to _geocoder#editPATCH /geocoder
to _geocoder#updatePUT /geocoder
to _geocoder#updateDELETE /geocoder
to _geocoder#delete
It also creates the following helpers:
geocoderPath()
returns/geocoder
newGeocoderPath()
returns/geocoder/new
editGeocoderPath(42)
returns/geocoder/42/edit
Multiple single resources can be defined at the same time:
router;
Nesting resources
Resources can be nested:
router;
In this case for example the express app can respond to GET /magazines/42/ads/7
path.
It adds to req.params
the following attributes:
magazineId
(in this case: 42)id
(in this case: 7)
Namespace
Routes can be namespaced. In this case the controller should exists in a directory with the same name of the namespace.
router;
In this case the article resource's actions are mapped to the following paths:
GET /admin/articles
GET /admin/articles/new
POST /admin/articles
GET /admin/articles/:id
GET /admin/articles/:id/edit
PATCH /admin/articles/:id
PUT /admin/articles/:id
DELETE /admin/articles/:id
Scopes
Routes can be also scoped. In this case the controller should exists in a directory with the same name of the scope.
routerscope'admin' { router;};
The difference with the namespace is that the routes paths don't have a prefix, but the controller lives inside a directory with the same name of the scope.
Fluent interface
Guidance router have a fluent interface, so it can be used in this way:
router scope'admin' {} ;
Notes
A modern version of node is required (due to harmony syntax). Actually tested with node v4.4.1 LTS.