Auto-route Express Promise
Summary
Auto-route Express Promise
routing made simple with a single place to update.
Installation
Install with npm
:
npm install --save autoroute-express-promise
Project Status
- Beta
- Active (June 26, 2015)
Example
Route Set Up
var router = express var routerallisUserAuthenticated moduleexports = router
Route/Controller Set Up
File Structure:
Note, that this is how I do my file structure. Any file structure is OK.
├── controllers/
├── api1/
│ └── index.js
├── api2/
│ └── index.js
└── api3/
└── index.js
Code Structure:
Note that you can do an array of routes also.
const PETS = '/pets' ID = 'petId' PET = PETS + '/:' + ID var routes: AutoRouteExpressPromiseRouteDefinition = route: PET methods: methodget methoddelete
Cleaner nested route syntax (version 0.2):
var routes = '/pets': _methods: methodpost ':id': methodget methoddelete '/cats': ':id': methodget '/hamsters': // Overrides cats because of the forward slash at beginning '/fish': // Overrides hamsters (hamsters bite) methodget
Example with two routes (this is the old way, you can still do it this way.):
const PETS = '/pets' ID = 'petId' PET = PETS + '/:' + ID var routes: AutoRouteExpressPromiseRouteDefinition = route: PETS methods: methodpost route: PET methods: methodget methoddelete
API
routes(options, glob[])
:
where options
:
any message?: void any
baseRoute: (Required) Uses route name (e.g., /api/myroute/:id
) and return
an express.js Router
. E.g.,
var router = expressRoutervar routerallisUserAuthenticated
message: (Optional) A function which passes the route name and method name (post, get, etc). Used for writing logs when route is called by server, e.g.,
{console}
where: o
is {routeName: '/api/myroute/:id', methodName: 'get'}
.
response: (Required) Used to wrap the result in some wrapper and send method, e.g.,
client
where glob
:
List of globs as specified in
require-glob
. E.g.,
['./controllers/**/index.js']
.
method
:
This is an enumeration of all the express.js
methods:
get, post, put, head, delete, options, trace, copy, lock, mkcol, move, purge, propfind, proppatch, unlock, report, mkactivity, checkout, merge, "m-search", notify, subscribe, unsubscribe, patch, search, connect
E.g.,
methodget // => 0method0 // => "get"