UNPKG

676 BJavaScriptView Raw
1//
2// # Load Routes
3//
4// * **routes**, path to json file containting the routes.
5// * **routers**, an object containing routers, keyed by method.
6// * **callback**, called when done or on error.
7//
8module.exports = function (routes, routers, callback) {
9 var r;
10 if (typeof routes === 'string') {
11 r = require(routes);
12 }
13 else {
14 r = routes;
15 }
16
17 for (var method in r) {
18 var router = routers[method];
19 if (!router) {
20 callback(new Error('Invalid HTTP method / missing router.'));
21 return;
22 }
23
24 var methodRoutes = r[method];
25 for (var path in methodRoutes) {
26 router.addRoute(path, methodRoutes[path]);
27 }
28 }
29
30 callback();
31};
32