express-routescan icon indicating copy to clipboard operation
express-routescan copied to clipboard

Enhancement Request: Route function

Open pavanputhra opened this issue 10 years ago • 5 comments

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]
   }
}

pavanputhra avatar Jun 05 '14 02:06 pavanputhra

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).';

pavanputhra avatar Jun 08 '14 06:06 pavanputhra

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.

anotheri avatar Jun 09 '14 17:06 anotheri

Yes, hash objects is more appropriate.

pavanputhra avatar Jun 10 '14 05:06 pavanputhra

Is this feature already included? The code seems unchanged and it's been a year.

ThirtyOne34 avatar Aug 11 '15 16:08 ThirtyOne34

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 }]

societymedia avatar Sep 10 '15 21:09 societymedia