Connect router with syntactic sugar
Basic Example
var express = app = expressconnectRouter = ; app; //OR load routes from a directoryapp; app;
Syntax
API
.router(route, ...);
Loads in the given routes. Can be a function, or a list of paths to load in. For example:
//scans a directory for routes; //recursively scans paths for routes; //scans for files ending in .route.js; //load in a func;
A route plugin should look something like this:
exports { router;}
Explicit Middleware
Explicit middleware is defined by using the ->
token. The basic example above uses explicit middleware, but here's another example:
router; //validate the user is logged in BEFORE returning the user profilerouter;
Implicit Middleware
Implicit middleware is used to extend existing routes. This is certainly useful if you want to drop in plugins which are specific to a given route. A good use case:
beta_user.js:
//Since we're in beta, extend the signup route. If the user has been invited, then//go onto the ORIGINAL signup route, otherwise return an error. //AFTER XXXX is out of beta, all we need to do is remove beta_user.js, and users//can signup without any constraints.router;
user.js
router;
Greedy Middleware
Greedy middleware allows you to wrap around entire paths. some/route/**
means any path after **
must go through this middleware. Here's an example:
//injected as middleware if -perm is provided. -perm tags flag that a route//requires authorizationrouter; //goes through permissions middlewarerouter; //does NOT go through perm middlewarerouter;
Note that greedy middleware is filterable based on the route tags. You can define anything you want. Here's another example:
//if POST is present, then automatically parse the bodyrouter; //body is automatically parsed for usrouter;