express-routescan
express-routescan copied to clipboard
Enhancement Request: Route function
Right now single route ("/api/abc") with more than one method ( GET, POST) can point to single function.
E. g.
module.exports = {
'/api/abc' : {
method: ['get','post'],
fn: functionForBothGetAndPost
}
}
It would be helpful if library provides a way to point to different function for different method.
E.g. Something like this:
module.exports = {
'/api/abc' : {
method: ['get','post'],
fn: [functionForGet, functionForPost]
}
}
Here is the patch for this enhancement. I have tested it once. If you feel I have made changes properly please commit these changes, so other can benefit
in file addRoute.js line 69 replace
} else if (action && typeof action.fn === 'function') {
with
} else if (action && (typeof action.fn === 'function' || action.fn instanceof Array )) {
in file addRoute.js line 51 replace
app[method](route, middleware || [], fn);
with
if(fn instanceof Array){
var indx = i < fn.length ? i : fn.length - 1;
app[method](route, middleware || [], fn[indx]);
} else {
app[method](route, middleware || [], fn);
}
in file inspectError.js line 30 replace
var noFnMsg = '\n\t' + 'It must be a function or an object with `fn` property (in this case `fn` should be a function).';
with
var noFnMsg = '\n\t' + 'It must be a function or an object with `fn` property (in this case `fn` should be a function or array of function).';
Yes, thank you, it seems ok for me, but I'm thinking of different lengths of arrays, for example route config which has 3 methods and 2 handlers in 'fn' array. It isn't obvious how should router work in this case. I guess that using hash objects instead of arrays for this purpose might be more handy and clear.
Yes, hash objects is more appropriate.
Is this feature already included? The code seems unchanged and it's been a year.
I implemented some similar to pavanpthra because i didnt see anything in the documentation supporting different actions per method. I forked this repo to try and add this enhancement. After looking at the tests I realized that this functionality is already included but not documented.
'/api/abc' :[{ method: ['get''], fn: functionForGet }, { method:['post'], fn:functionForPost }]