Search code examples
node.jsexpressroutesexpress-router

How to use multiple route to invoke same method in express.js?


Right now I have two routes, invoking the same method.

Let's say the method's name is myMethod And two routes are / and /:params

router.get("/", myMethod);
router.get("/:param", myMethod);

Since in both cases I have to invoke the same method, I want to use a single route to invoke the myMethod. Something like this,

router.get('/' or '/:param', methodName);

If it is possible, then how can I do this?


Solution

  • You can use an array :

    router.get(['/', '/:param'], myMethod);