express-remove-route
Advanced tools
Comparing version 0.1.1 to 1.0.0
79
index.js
@@ -1,40 +0,31 @@ | ||
module.exports = function removeRoute(app, path) { | ||
var found, route, stack, idx; | ||
found = findRoute(app, path); | ||
route = found.route; | ||
stack = found.stack; | ||
var util=require('util'); | ||
var _=require('underscore'); | ||
if (!route) return false; | ||
idx = stack.indexOf(route); | ||
stack.splice(idx, 1); | ||
return true; | ||
}; | ||
module.exports.findRoute = findRoute; | ||
function findRoute(app, path) { | ||
var route, stack; | ||
stack = app._router.stack; | ||
function _findRoute(path) { | ||
stack.forEach(function(layer) { | ||
if (!layer) return; | ||
if (layer && !layer.match(path)) return; | ||
if (['query', 'expressInit'].indexOf(layer.name) != -1) return; | ||
if (layer.name == 'router') { | ||
stack = layer.handle.stack; | ||
_findRoute(trimPrefix(path, layer.path)); | ||
} else { | ||
route = layer; | ||
function _findRoute(path,stack) { | ||
var count=0; | ||
var routes=[]; | ||
stack.forEach(function(layer) { | ||
if (!layer) return; | ||
if (layer && !layer.match(path)) return; | ||
if (['query', 'expressInit'].indexOf(layer.name) != -1) return; | ||
if (layer.name == 'router') { | ||
routes=routes.concat(_findRoute(trimPrefix(path, layer.path),layer.handle.stack)); | ||
} else { | ||
if (layer.name == 'bound ') { | ||
routes.push({route: layer || null, stack: stack}); | ||
} | ||
}); | ||
} | ||
} | ||
}); | ||
return routes; | ||
} | ||
_findRoute(path, stack); | ||
if (!route) return null; | ||
return {route: route, stack: stack}; | ||
function findRoute(app, path) { | ||
var stack; | ||
stack = app._router.stack; | ||
return (_findRoute(path, stack)); | ||
} | ||
@@ -46,1 +37,27 @@ | ||
} | ||
module.exports = function removeRoute(app, path, method) { | ||
var found, route, stack, idx; | ||
found = findRoute(app, path); | ||
found.forEach(function(layer) { | ||
route = layer.route; | ||
stack = layer.stack; | ||
if (route) { | ||
if(_.isEmpty(method)){ // if no method delete all resource with the given path | ||
idx = stack.indexOf(route); | ||
stack.splice(idx, 1); | ||
}else if(JSON.stringify(route.route.methods).toUpperCase().indexOf(method.toUpperCase())>=0){ // if method defined delete only the resource with the given ath and method | ||
idx = stack.indexOf(route); | ||
stack.splice(idx, 1); | ||
} | ||
} | ||
}); | ||
return true; | ||
}; | ||
module.exports.findRoute = findRoute; |
{ | ||
"name": "express-remove-route", | ||
"version": "0.1.1", | ||
"version": "1.0.0", | ||
"description": "Remove a route in express at runtime.", | ||
"main": "index.js", | ||
"scripts": { | ||
}, | ||
"scripts": {}, | ||
"repository": { | ||
@@ -9,0 +8,0 @@ "type": "git", |
@@ -31,2 +31,52 @@ # express-remove-route | ||
# Reference | ||
## removeRoute(app, resourcePath, resourceMethod); | ||
This is the function that remove a resource route on the fly | ||
The param `app` is the express application(app) on which you want remove the resource. | ||
the param `resourcePath` is the resource path to remove on the fly | ||
the param `resourceMethod` is the resource method with a given path to remove on the fly. If null or undefined all | ||
routes with the given path are removed. | ||
# Examples | ||
## Remove all resources with a given path | ||
var removeRoute = require('express-remove-route'); | ||
var app = express(); | ||
var router = express.Router(); | ||
router.get('/remove/me', function(res, res) { | ||
res.send('I should not be here'); // removed | ||
}); | ||
router.put('/remove/me', function(res, res) { | ||
res.send('I should not be here'); // removed | ||
}); | ||
app.use('/foo', router); | ||
removeRoute(app, '/foo/remove/me'); // all routes with a path /foo/remove/me are removed | ||
## Remove all resources with a given path and method | ||
var removeRoute = require('express-remove-route'); | ||
var app = express(); | ||
var router = express.Router(); | ||
router.get('/remove/me', function(res, res) { | ||
res.send('I should not be here'); // removed | ||
}); | ||
router.put('/remove/me', function(res, res) { | ||
res.send('I should be here'); // not removed | ||
}); | ||
app.use('/foo', router); | ||
removeRoute(app, '/foo/remove/me','get'); // all routes with a path /foo/remove/me in method get are removed | ||
# Caveat emptor | ||
@@ -40,1 +90,7 @@ This module has been tested against Express 4.13.3. In theory, | ||
structures that may be subject to change. | ||
Contributors | ||
------------ | ||
Brennan Cheung ([git@brennancheung.com](mailto:git@brennancheung.com)) | ||
Alessandro Romanino ([a.romanino@gmail.com](mailto:a.romanino@gmail.com)) |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
6540
46
0
95