Shams Nahid
Shams Nahid

Reputation: 6559

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?

Upvotes: 0

Views: 628

Answers (1)

ElJackiste
ElJackiste

Reputation: 4621

You can use an array :

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

Upvotes: 5

Related Questions