-
Notifications
You must be signed in to change notification settings - Fork 2
Routing
# routes.json
{
"": [
"middleware.contextualizer", "middleware.static", "middleware.logger",
"middleware.logwriter", "middleware.query", "middleware.bodyParser", "middleware.cookieParser",
"middleware.session", "middleware.csrf", "middleware.router", "middleware.errorHandler"
],
"/": {
"get": ["middleware.dispatcher"]
},
"/register": {
"post": ["middleware.validator", "middleware.auth.register"],
"get": ["middleware.dispatcher"]
},
"/login": {
"post": ["middleware.validator", "middleware.auth.login"]
},
"/logout": {
"all": ["middleware.auth.logout"]
}
}
Routes are written in JSON or YAML in a file passed to the routes member of the argument of the Rig constructor. Routing inherits heavily from Express and Rig just adds a thin configuration layer on top of it.
The keys of the routes configuration object are the query paths that are going to be routed. Inside the value associated with a path, the keys are the http verbs ("get","post", "put", etc. but also "all") that we want to route queries for. The value associated with each verb is an Array listing the names of the registered middleware resources that are going to be traversed by the request when it has the given verb and path.
E.g.
/** server.js */
var Rig = require('rig'),
rig = new Rig({
config: 'config.json',
routes: 'routes.json'
});
rig.register('middleware.myMiddleware', require('middleware/myMiddleware'));# routes.json
{
"": ["middleware.router"], // read more about this special path in the next paragraph
"/hello": {
"get": ["middleware.myMiddleware"]
}
}In this example, each request for GET /hello will call the middleware returned by the function exported by the module at 'middleware/myMiddleware' on the filesystem.
Tip: In the underlying implementation, the verb is going to be called on the Express app, with the path and named middlewares as arguments.
##Middleware
Middleware that needs to be used for every single request can be listed in a special "" path. They will be invoked regardless of the verb.
Tip: This array will be passed to the app.use method of the Express app without a path argument.
In fact, if we want a path routing, the "middleware.router" middleware needs to be listed in this special path. It is important to note that the path-routed middleware lists are executed at the position the "middleware.router" is listed in the special "" path. You can think of path-routed lists as sublists of the special 'universal' "" list.
E.g.
{
"": ["middleware.bodyParser", "middleware.router", "middleware.errorHandler"]
"/hello": {
"get": ["middleware.myMiddleware", "middleware.yourMiddleware"]
},
"/world": {
"get": ["middleware.myMiddleware"]
}
}In the above example, GET /hello will go in order through: "middleware.bodyParser", "middleware.myMiddleware", "middleware.yourMiddleware", "middleware.errorHandler"
but GET /world will go through: "middleware.bodyParser", "middleware.myMiddleware", "middleware.errorHandler"