app-router
Advanced tools
Comparing version 0.1.2 to 0.1.3
70
index.js
@@ -1,4 +0,4 @@ | ||
var fs=require("fs"), | ||
path=require("path"), | ||
util=require("util"), | ||
var fs = require("fs"), | ||
path = require("path"), | ||
util = require("util"), | ||
showRoute = false, | ||
@@ -12,3 +12,3 @@ throwError = true, | ||
function (a, b) { | ||
var interObject=iObj; | ||
var interObject = iObj; | ||
b.split(".").forEach(function(item){ | ||
@@ -34,6 +34,6 @@ if(typeof interObject[item] != 'undefined'){ | ||
function (a, b) { | ||
var interObject=iObj; | ||
var interObject = iObj; | ||
b.split(".").forEach(function(item){ | ||
if(typeof interObject[item] != 'undefined'){ | ||
interObject=interObject[item]; | ||
interObject = interObject[item]; | ||
} | ||
@@ -54,6 +54,6 @@ }); | ||
var startRouting= function(routes, expressApp, mainPath){ | ||
var _VARIABLE= inrePolateVar(routes); | ||
var startRouting = function(routes, expressApp, mainPath){ | ||
var _VARIABLE = inrePolateVar(routes); | ||
Object.keys(routes).forEach(function(item){ | ||
if(item.match(/get|post|put|delete|options|resource/i)){ | ||
if(item.match(/get|post|put|delete|del|options|patch|resource/i)){ | ||
routeMethod(routes[item], item, _VARIABLE, expressApp, mainPath); | ||
@@ -65,5 +65,5 @@ } | ||
var inrePolateVar = function(routes){ | ||
var _VARIABLE= routes.VARIABLE || routes.variable || {}; | ||
var _VARIABLE = routes.VARIABLE || routes.variable || { }; | ||
Object.keys(_VARIABLE).forEach(function(item){ | ||
_VARIABLE[item]= interPolateStr(_VARIABLE[item], _VARIABLE); | ||
_VARIABLE[item] = interPolateStr(_VARIABLE[item], _VARIABLE); | ||
}); | ||
@@ -84,3 +84,3 @@ return _VARIABLE; | ||
Object.keys(routes).forEach(function(item){ | ||
var controller =util.isArray(routes[item])? | ||
var controller = util.isArray(routes[item])? | ||
interPolateStrArray(routes[item], _VARIABLE): | ||
@@ -93,4 +93,4 @@ interPolateStr(routes[item], _VARIABLE); | ||
var routeThis = function(route, methodType, controllers, expressApp, _VARIABLE, mainPath){ | ||
methodType=methodType.toLowerCase(); | ||
if(methodType=="resource"){ | ||
methodType = methodType.toLowerCase(); | ||
if(methodType === "resource"){ | ||
return resourceThis(route, controllers, expressApp, _VARIABLE, mainPath); | ||
@@ -100,7 +100,7 @@ } | ||
if(util.isArray(controllers)){ | ||
actions=controllers.map(function(item){ | ||
actions = controllers.map(function(item){ | ||
return resolveThisMethod(item, mainPath); | ||
}); | ||
}else{ | ||
actions=[resolveThisMethod(controllers, mainPath)]; | ||
actions = [resolveThisMethod(controllers, mainPath)]; | ||
} | ||
@@ -111,3 +111,3 @@ | ||
}else{ | ||
if(methodType=="delete"){methodType="del"} | ||
//if(methodType === "delete"){methodType = "del"} | ||
actions.unshift(route); | ||
@@ -121,11 +121,11 @@ expressApp[methodType].apply(expressApp, actions); | ||
var resolveThisMethod =function(item, mainPath){ | ||
var c_a=item.split(":"); | ||
var resolveThisMethod = function(item, mainPath){ | ||
var c_a = item.split(":"); | ||
if(c_a.length != 2){ | ||
throw new Error("Invalid routing: "+ item); | ||
} | ||
var controller= require(path.join(mainPath, c_a[0])); | ||
var controller = require(path.join(mainPath, c_a[0])); | ||
c_a[1].split(".").forEach(function(item){ | ||
if(typeof controller[item] != 'undefined'){ | ||
controller=controller[item]; | ||
controller = controller[item]; | ||
}else{ | ||
@@ -148,4 +148,4 @@ if(throwError){ | ||
var resourceThis= function(route, controller, expressApp, _VARIABLE, mainPath){ | ||
var resourcing={ | ||
var resourceThis = function(route, controller, expressApp, _VARIABLE, mainPath){ | ||
var resourcing = { | ||
index:{ | ||
@@ -200,3 +200,3 @@ verb:"get", | ||
if(useFormat){ | ||
expressApp[resourcing[item].verb](appRoute+".:format", action); | ||
expressApp[resourcing[item].verb](appRoute + ".:format", action); | ||
} | ||
@@ -216,8 +216,8 @@ expressApp[resourcing[item].verb](appRoute, action); | ||
var lines = text.split('\n'); | ||
var strToObj={}; | ||
var strToObj = { }; | ||
lines.forEach(function(line, num){ | ||
var cmds =line.split(/[\s\t]+/); | ||
var cmds = line.split(/[\s\t]+/); | ||
if(cmds.length < 3){ | ||
if(cmds.length > 1){ | ||
throw new Error("Invalid Routing line number:" + (num+1) + " " +line); | ||
throw new Error("Invalid Routing line number:" + (num + 1) + " " + line); | ||
} | ||
@@ -229,3 +229,3 @@ return false; | ||
strToObj[verb] = strToObj[verb] || {}; | ||
strToObj[verb][route]= (cmds.length > 1 ? cmds : cmds[0]); | ||
strToObj[verb][route] = (cmds.length > 1 ? cmds : cmds[0]); | ||
}); | ||
@@ -235,7 +235,7 @@ return strToObj; | ||
var loadText= function(filePath, mainPath){ | ||
var loadText = function(filePath, mainPath){ | ||
return fs.readFileSync(path.join(mainPath, filePath), "utf8"); | ||
} | ||
var router=module.exports= function(expressApp){ | ||
var router = module.exports = function(expressApp){ | ||
return new (function(){ | ||
@@ -259,3 +259,3 @@ this.route = function(filePath){ | ||
if(typeof bool == "undefined"){ | ||
bool =true; | ||
bool = true; | ||
} | ||
@@ -267,3 +267,3 @@ showRoute = Boolean(bool); | ||
if(typeof bool == "undefined"){ | ||
bool =true; | ||
bool = true; | ||
} | ||
@@ -274,3 +274,3 @@ throwError = Boolean(bool); | ||
this.setCWD = function(mainPath){ | ||
this.path=mainPath; | ||
this.path = mainPath; | ||
return this; | ||
@@ -280,3 +280,3 @@ }, | ||
if(typeof handle == "function"){ | ||
handler=handle; | ||
handler = handle; | ||
} | ||
@@ -287,3 +287,3 @@ return this; | ||
if(typeof bool == "undefined"){ | ||
bool =true; | ||
bool = true; | ||
} | ||
@@ -290,0 +290,0 @@ useFormat = Boolean(bool); |
{ | ||
"name": "app-router", | ||
"version": "0.1.2", | ||
"version": "0.1.3", | ||
"description":"Routing for expressjs", | ||
@@ -5,0 +5,0 @@ "keywords":["express router", "application router", "app-router", "yaml-router", "yml-router", "yaml routing", "express", "connect", "router", "routing", "json router", "json-router"], |
@@ -45,3 +45,2 @@ [![NPM](https://nodei.co/npm/app-router.svg?downloads=true&downloadRank=true)](https://nodei.co/npm/app-router/) | ||
// routing example in express | ||
method = method.replace("delete","del"); | ||
actions.unshift(route); | ||
@@ -48,0 +47,0 @@ app[method].apply(app, actions); |
15245
202