Categories
JavaScript Answers

How to add a single routing handler for multiple routes in a single line with Node.js Express.js?

Spread the love

To add a single routing handler for multiple routes in a single line with Node.js Express.js, we call the route method with an array of routes.

For instance, we write

app.get(
  [
    "/test",
    "/alternative",
    "/bar*",
    "/foo/:farcus/",
    "/hoop(|la|lapoo|lul)/poo",
  ],
  (request, response) => {}
);

to create a get route handler that accepts requests from the URLs listed in the array.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *