app-router
Advanced tools
Comparing version 0.0.5 to 0.0.6
58
index.js
var fs=require("fs"), | ||
path=require("path"), | ||
util=require("util"); | ||
@@ -24,7 +25,7 @@ | ||
var startRouting= function(routes, expressApp){ | ||
var startRouting= function(routes, expressApp, mainPath){ | ||
var _GLOBAL= routes.GLOBAL || {}; | ||
Object.keys(routes).forEach(function(item){ | ||
if(item.match(/get|post|put|delete|options|resource/i)){ | ||
routeMethod(routes[item], item, _GLOBAL, expressApp); | ||
routeMethod(routes[item], item, _GLOBAL, expressApp, mainPath); | ||
} | ||
@@ -34,13 +35,12 @@ }); | ||
var routeMethod = function(routes, methodType, _GLOBAL, expressApp){ | ||
var routeMethod = function(routes, methodType, _GLOBAL, expressApp, mainPath){ | ||
if(util.isArray(routes)){ | ||
return routes.forEach(function(thisRoute){ | ||
processRoute(thisRoute, methodType, _GLOBAL, expressApp); | ||
processRoute(thisRoute, methodType, _GLOBAL, expressApp, mainPath); | ||
}); | ||
} | ||
processRoute(routes, methodType, _GLOBAL, expressApp); | ||
processRoute(routes, methodType, _GLOBAL, expressApp, mainPath); | ||
} | ||
var processRoute = function(routes, methodType, _GLOBAL, expressApp ){ | ||
var processRoute = function(routes, methodType, _GLOBAL, expressApp, mainPath){ | ||
Object.keys(routes).forEach(function(item){ | ||
@@ -50,16 +50,18 @@ var controller =util.isArray(routes[item])? | ||
interPolate(routes[item], _GLOBAL); | ||
routeThis(item, methodType, controller, expressApp, _GLOBAL); | ||
routeThis(item, methodType, controller, expressApp, _GLOBAL, mainPath); | ||
}); | ||
} | ||
var routeThis = function(route, methodType, controllers, expressApp, _GLOBAL){ | ||
var routeThis = function(route, methodType, controllers, expressApp, _GLOBAL, mainPath){ | ||
methodType=methodType.toLowerCase(); | ||
if(methodType=="resource"){ | ||
return resourceThis(route, controllers, expressApp, _GLOBAL); | ||
return resourceThis(route, controllers, expressApp, _GLOBAL, mainPath); | ||
} | ||
var actions; | ||
if(util.isArray(controllers)){ | ||
actions=controllers.map(resolveThisMethod) | ||
actions=controllers.map(function(item){ | ||
return resolveThisMethod(item, mainPath); | ||
}); | ||
}else{ | ||
actions=[resolveThisMethod(controllers)]; | ||
actions=[resolveThisMethod(controllers, mainPath)]; | ||
} | ||
@@ -69,6 +71,5 @@ actions.unshift(route); | ||
expressApp[methodType].apply(expressApp, actions); | ||
//console.log(methodType,route); | ||
} | ||
var resolveThisMethod =function(item){ | ||
var resolveThisMethod =function(item, mainPath){ | ||
var c_a=item.split(":"); | ||
@@ -78,3 +79,3 @@ if(c_a.length != 2){ | ||
} | ||
var controller= require(c_a[0]); | ||
var controller= require(path.join(mainPath, c_a[0])); | ||
c_a[1].split(".").forEach(function(item){ | ||
@@ -90,3 +91,3 @@ if(typeof controller[item] != 'undefined'){ | ||
var resourceThis= function(route, controller, expressApp, _GLOBAL){ | ||
var resourceThis= function(route, controller, expressApp, _GLOBAL, mainPath){ | ||
var resourcing={ | ||
@@ -130,3 +131,3 @@ index:{ | ||
Object.keys(resourcing).forEach(function(item){ | ||
var action = require(controller)[item]; | ||
var action = require(path.join(mainPath,controller))[item]; | ||
if(typeof action != "function"){ | ||
@@ -137,17 +138,22 @@ throw new Error("cant find action "+ item + " in controller " + controller); | ||
expressApp[resourcing[item].verb](appRoute+".:format", action); | ||
expressApp[resourcing[item].verb](appRoute, action); | ||
//console.log(resourcing[item].verb,appRoute+".:format"); | ||
//console.log(resourcing[item].verb,appRoute); | ||
expressApp[resourcing[item].verb](appRoute, action); | ||
}); | ||
} | ||
module.exports= function(expressApp){ | ||
return { | ||
route:function(jsonPath){ | ||
var routes = require(jsonPath); | ||
startRouting(routes, expressApp); | ||
var router=module.exports= function(expressApp){ | ||
return new (function(){ | ||
this.route = function(jsonPath){ | ||
if(!this.path){ | ||
throw new Error("set working directory first , use setCWD() method"); | ||
} | ||
var routes = require(path.join(this.path, jsonPath)); | ||
startRouting(routes, expressApp, this.path); | ||
}, | ||
this.setCWD = function(mainPath){ | ||
this.path=mainPath; | ||
return this; | ||
} | ||
} | ||
})(); | ||
} | ||
{ | ||
"name": "app-router", | ||
"version": "0.0.5", | ||
"version": "0.0.6", | ||
"description":"Routing for expressjs", | ||
@@ -5,0 +5,0 @@ "keywords":["express router", "application router", "app-router", "express", "connect", "router", "routing", "json to router", "json-router"], |
@@ -12,6 +12,11 @@ app-router | ||
var router = require("app-router"); | ||
router(app).route("./route.json"); | ||
router(app).setCWD(__dirname).route("./routes/route.json"); | ||
``` | ||
setting current working directory (CWD) relative to all controllers path and route.json | ||
```javascript | ||
setCWD(__dirname) | ||
``` | ||
sample <b> route.json </b> | ||
@@ -23,3 +28,3 @@ | ||
"cp":"./controllers/my_controller", | ||
"other_controller":"./controllers/other_controller.js" | ||
"other_controller":"./controllers/other_controller.js" | ||
}, | ||
@@ -73,3 +78,3 @@ "GET":{ | ||
``` | ||
Basic syntax for config.json | ||
Basic syntax for route.json | ||
@@ -76,0 +81,0 @@ ```javascript |
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
8806
202
96