What
Register your route as natural as Express' way, PLUS adding name/label to it.
Then you can generate URL by calling urlFor
.
Example
Either:
// In index.jsrouter;router; router;router;
or:
// In index.jsrouter;router; // In path/to/module/user/index.jsuserRouter;userRouter; // In path/to/module/article/index.jsarticleRouter;articleRouter;
You can:
router -> '/users/0'router -> '/users/2/edit'router -> '/articles'router -> '/articles/love-craft'
Sample Project
See route-label-sample for sample integration on express.js.
Why
Define route in a way you define constants (name => route), because:
- No need to remember the route's URL patterns, just the name to generate URL. Useful for large application with many end points.
- Easy to change URL patterns, just in the "constant" definition.
Features
- Define route as natural as Express' way.
- Mount submodule routes using
use
method, and get the whole route names respect the module - submodule structure by namespaces. - You still can apply middlewares in multiple lines flexibly.
- Get the route table and you can decide what to do with it: pass to front end, finding route name based on pattern, etc.
How to use
Installing
npm install --save route-label
Registering Routes
Basic
In the top level express app, wrap the express or express.Router instance with this library:
var app = ;var router = app;
Then you can define any routing with this signature:
router;
name
is optional. If provided, this route will be registered in the route table and you can generate its URL using urlFor
.
It may contain alphanumeric, dot, dashes, and underscore.
METHOD
is the router's method, such as get
, post
, put
, all
, or use
.
At the end of your outermost route definitions, call:
router;
This will process all registered route above and store it for future URL generation (urlFor
).
You only need to call it once.
Example:
var router = app; routerall'/*' middleware;router;router; router;
Mounting Submodule
If you mount another "submodule" with use
keyword, the given name will become prefix for all routes inside that submodule.
Without giving name, the nested routes inside the submodule won't be registered as named route.
Prefixes will be concatenated with dot ('.') character. It is also possible to have subsubmodule and subsubsubmodule.
Example, in your /routes/index.js
:
var app = ;var router = app; router;
In /path/to/module/article/index.js
:
var appRouter = ; // Use express.Router() instead of express()var router = appRouter; router;router; moduleexports = appRouter; // Return the express.Router() instance
Now you get 'article.list' and 'article.detail' routes defined.
If you provide empty string as names, they will be ignored in the prefix. For example, if you did this:
router;
We get 'list' and 'detail' routes defined, instead of '.list' and '.detail'
Generate URL
.urlFor
To generate URL, it is not necessary for route-label to wrap app instance.
var router = ; // No need to wrap `app` here
Then you can call urlFor
with this signature:
Where paramObj
is object containing values to be plugged to the URL, and queryObj
is object which will be serialized as query string.
Example:
/*Consider this route definitions: 'article.list' => '/articles' 'article.detail' => '/articles/:title'*/ // Returns /articles/cool-guyrouter; // Returns /articles/cool-guy?mode=showrouter; // Returns /articles/cool-guy?mode=show&tag=man&tag=people&tag=moneyrouter; // Returns /articles?order=ascrouter; // Throw error, missing `title`router;router;
.absoluteUrlFor
To generate absolute URL, set the baseUrl
with:
router;
This works globally, so you only need to set it once. In the main routing file perhaps?
After that you can call absoluteUrlFor
anywhere.
Example:
router; // Returns https://www.cermati.com/articles/cool-guyrouter;
.getRouteTable
After buildRouteTable
, you can call this anywhere using route-label (with or without wrapping app).
/*Consider this route definitions: 'article.list' => '/articles' 'article.detail' => '/articles/:title'*/ router;
Will return:
'article.list': '/articles' 'article.detail': '/articles/:title'
FAQ
Has anyone used this on production server?
Yes, the birthplace of this library, Cermati, and our other projects using Node.js. We have this on production server running since October 2015.
Why bother creating this library?
We reviewed other libraries for named route, but none of them suites our needs, especially for submodule routing. So we decided to build our own solution. Battle tested in production, we proceed to release this as open source.
How does it work internally?
It wraps Express' routing, attaching name in the routes before calling actual Express' routing function. When
.buildRouteTable
is called, the attached names are traversed in pre-order fashion. The result is stored in table and used for future URL generation.
Can it be used as template helper?
You can, if the template engine allow creation of custom helper. For example in Handlebars.js, you can define custom helper which calls
urlFor
.
License
MIT