Why isn't the /* or * route working in my JavaScript Express application?

I’m just starting to learn about Express, and I’m encountering some issues. While the /about route is functioning properly, I’m having problems with the * and /* routes, resulting in errors. I followed a tutorial, but my code doesn’t seem to work the same way. Here’s my current setup:

app.get("/about", (req, res) => {
  res.send("you are in about path");
});

app.get("*", (req, res) => {
  res.send("you are in a path that doesn't exist");
});

When I run the code, I receive this error:

Error: node_modules\path-to-RegExp\dist\index.js:73
     throw new TypeError (`Missing parameter name at ${i}: ${DEBUG_URL}`);
     ^

TypeError: Missing parameter name at 1: https://git.new/pathToRegexpError

I’ve tried to implement this alternative code:

app.all("*", (req, res) => {
  res.status(404).send("you are in a path that doesn't exist");
});

However, it still doesn’t resolve the issue. I need to ensure I can provide responses for all other undefined routes.

hmm weird error, ive seen this before tho. check if you have any other routes defined after the * route - express processes routes in order so the wildcard should always be last. also make sure theres no typos in route paths above it, sometimes a malformed route can cause this pathToRegexp error.

I ran into something similar when learning Express. That pathToRegexp error usually means there’s a malformed route parameter somewhere in your code, not necessarily with the wildcard route itself. Look through your entire app file for any route definitions that might have incomplete parameter syntax - things like /users/: without a parameter name after the colon, or routes with special characters that aren’t properly escaped. Sometimes copying code from tutorials can introduce hidden characters too. Try commenting out all your routes except the basic ones and add them back one by one to isolate which route is causing the issue. The wildcard route code you have is actually correct, so the problem is definitely elsewhere in your routing setup.

The pathToRegexp error typically suggests there’s a syntax issue with one of your route definitions. The problem likely isn’t with your wildcard route itself, but rather with another route you might have defined elsewhere. Carefully review all route definitions for any missing colons before parameters; for instance, ensure you’re using app.get('/user/:id', ...) and not app.get('/user/id', ...). Also, double-check for any malformed parameter syntax like /user/: without a proper parameter name. The error message you’re receiving points to a specific line in the path-to-regexp package, which Express uses to parse routes. Once you correct any malformed routes, your wildcard route should function correctly.

kept running into this problem in my apps, it’s related to the differences between express 4 and 5, see official guide. in short, use /*splat or /{*splat}