Categories
JavaScript Answers

How to add wildcard routing to cover everything under and including a path with Node.js Express.js?

Spread the love

To add wildcard routing to cover everything under and including a path with Node.js Express.js, we can add multiple routes.

For instance, we write

const express = require("express");
const app = express.createServer();

const fooRoute = (req, res, next) => {
  res.end("Foo Route\n");
};

app.get("/foo*", fooRoute);
app.get("/foo", fooRoute);

app.listen(3000);

to add the /foo* route to handle any URLs that starts with /foo.

And we add the /foo route to handle request with URL /foo.

We call fooRoute to handle both kinds of requests.

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 *