UNPKG

1.53 kBJavaScriptView Raw
1"use strict";
2
3/**
4 * Declaratively initializes routes in an express app
5 */
6var debug = require("debug")("jefferson");
7
8module.exports = function (app, conf) {
9 if (!app) {
10 throw new Error("app parameter must be supplied");
11 }
12 if (!conf) {
13 throw new Error("application configuration must be supplied");
14 }
15
16 var proxies = conf.proxies;
17
18 var possiblyProxy = function (middleware) {
19 if (!proxies || !proxies.length) {
20 return middleware;
21 }
22 return middleware.map(function (mw) {
23 var lastProxy = proxies[proxies.length - 1].init(mw);
24 for (var i = proxies.length - 2; i >= 0; i--) {
25 lastProxy = proxies[i].init(lastProxy);
26 }
27 return lastProxy;
28 });
29 };
30
31 var wireRoute = function (routeName, route) {
32 var method = route.method.toLowerCase(),
33 path = route.path,
34 middleware = possiblyProxy(route.middleware);
35 //jscs:disable
36 debug("routing " + routeName + " - " + method + " " + path + " - " + middleware.length + " middlewares");
37 //jscs:enable
38 app[method](path, middleware);
39 };
40 var wireRoutes = function () {
41 var routeNames = Object.keys(conf.routes);
42 //jscs:disable
43 debug("wiring " + routeNames.length + " routes");
44 //jscs:enable
45 routeNames.forEach(function (routeName) {
46 return wireRoute(routeName, conf.routes[routeName]);
47 });
48 };
49
50 wireRoutes();
51};
\No newline at end of file