expressjs-route-mapper
HTTP dynamic routing with Express.js
About
The motivation for this module is to provide a high-level abstraction of routes in express.js. It is written in JavaScript, and is 100% MIT licensed.
- a simple, easy method for generating restful routes for a model.
- easy declaration of your "root" or "home" route.
- support virtualhost, domain specific routes
- support condition inheritance
- optional definition of a "before wrapper" function, which is called before request handling is made.
- optional support middleware
Install
npm install expressjs-route-mapper --save
Supported HTTP methods:
Method Scope Semantics
GET collection Retrieve all resources in a collection
GET resource Retrieve a single resource
HEAD collection Retrieve all resources in a collection (header only)
HEAD resource Retrieve a single resource (header only)
POST collection Create a new resource in a collection
PUT resource Update a resource
PATCH resource Update a resource
DELETE resource Delete a resource
COPY resource Clone/Copy of an existing resource.
PURGE resource pick out an object from the cache and discard
TRACE resource TRACE allows the client to see testing or diagnostic information.
OPTIONS any Return available HTTP methods and other options
Examples
Here is an example on how to use it:
Basic route
express = ; app = ; ; app;
These are the routes that will get created with the code above:
Method URL
GET /users/:user_id/tables/:table_id/stories
POST /users/:user_id/tables/:table_id/stories
DELETE /users/:user_id/tables/:table_id/stories
PUT /users/:user_id/tables/:table_id/stories
Basic route with before wrapper
Here we have route [IndexRoute] with before callback [IndexRoute.before] which run before the main route IndexRoute. This is useful for security check or validate a user before render the main route
- note: before condition inherits down to child routes
express = ; app = ; ; { return 'Hello World' } IndexRoute{ return 'Hello World' } app;
Basic route with base root
Declare your root or home url with the base options:
app;
These are the routes that will get created with that method call:
Method URL
GET /product
GET /product/category
Route mapping with multiple layers
express = ; app = ; ; app;
These are the routes that will get created with that method call:
Method URL
GET /product
POST /product
GET /product/byId
Delete /product/byId
Virtual Host
domain specific routes
express = ; app = ; ; myDomainServer = ; myDomainServer app; app;
These are the routes that will get created with that method call:
Method URL Domain
GET /my-route http://one.lvh.me/my
GET /your-route http://your-domain/your-domain
Supported domain patterns:
lvhme //domain www wwwlvhme //sub-domain www *lvhme //any sub-domain
Todo
- ways to customize, override and extend the generated restful routes
Notes
Inspired by express.js examples