From 51840e139559395ae150b3fd2840b56178b0c447 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kamil=20My=C5=9Bliwiec?= Date: Wed, 22 Jan 2025 10:29:05 +0100 Subject: [PATCH] docs: note about express named wildcard parameters --- content/controllers.md | 2 ++ content/middlewares.md | 2 ++ content/migration.md | 4 ++-- 3 files changed, 6 insertions(+), 2 deletions(-) diff --git a/content/controllers.md b/content/controllers.md index cc3fb32ba3..ac489e816c 100644 --- a/content/controllers.md +++ b/content/controllers.md @@ -202,6 +202,8 @@ findAll() { The `'abcd/*'` route path will match `abcd/`, `abcd/123`, `abcd/abc`, and so on. The hyphen ( `-`) and the dot (`.`) are interpreted literally by string-based paths. +This approach works on both Express and Fastify. However, with the latest release of Express (v5), the routing system has become more strict. In pure Express, you must use a named wildcard to make the route work—for example, `abcd/*splat`, where `splat` is simply the name of the wildcard parameter and has no special meaning. You can name it anything you like. That said, since Nest provides a compatibility layer for Express, you can still use the asterisk (`*`) as a wildcard. + When it comes to asterisks used in the **middle of a route**, Express requires named wildcards (e.g., `ab{{ '{' }}*splat}cd`), while Fastify does not support them at all. #### Status code diff --git a/content/middlewares.md b/content/middlewares.md index f75592d2e1..8d166d0881 100644 --- a/content/middlewares.md +++ b/content/middlewares.md @@ -137,6 +137,8 @@ forRoutes({ }); ``` +> info **Hint** `splat` is simply the name of the wildcard parameter and has no special meaning. You can name it anything you like, for example, `*wildcard`. + The `'abcd/*'` route path will match `abcd/1`, `abcd/123`, `abcd/abc`, and so on. The hyphen ( `-`) and the dot (`.`) are interpreted literally by string-based paths. However, `abcd/` with no additional characters will not match the route. For this, you need to wrap the wildcard in braces to make it optional: ```typescript diff --git a/content/migration.md b/content/migration.md index 9a023c0abc..469c5818be 100644 --- a/content/migration.md +++ b/content/migration.md @@ -12,7 +12,7 @@ After years of development, Express v5 was officially released in 2024 and becam One of the most notable updates in Express v5 is the revised path route matching algorithm. The following changes have been introduced to how path strings are matched with incoming requests: -- The wildcard `*` must have a name, matching the behavior of parameters :, use `/*splat` or `/{{ '{' }}*splat}` instead of `/*` +- The wildcard `*` must have a name, matching the behavior of parameters: use `/*splat` or `/{{ '{' }}*splat}` instead of `/*`. `splat` is simply the name of the wildcard parameter and has no special meaning. You can name it anything you like, for example, `*wildcard` - The optional character `?` is no longer supported, use braces instead: `/:file{{ '{' }}.:ext}`. - Regexp characters are not supported. - Some characters have been reserved to avoid confusion during upgrade `(()[]?+!)`, use `\` to escape them. @@ -38,7 +38,7 @@ findAll() { } ``` -> warning **Warning** Note that `*splat` is a named wildcard that matches any path without the root path. If you need to match the root path as well (`/users`), you can use `/users/{{ '{' }}*splat}`, wrapping the wildcard in braces (optional group). +> warning **Warning** Note that `*splat` is a named wildcard that matches any path without the root path. If you need to match the root path as well (`/users`), you can use `/users/{{ '{' }}*splat}`, wrapping the wildcard in braces (optional group). Note that `splat` is simply the name of the wildcard parameter and has no special meaning. You can name it anything you like, for example, `*wildcard`. Similarly, if you have a middleware that runs on all routes, you may need to update the path to use a named wildcard: