Skip to content

Commit

Permalink
docs: note about express named wildcard parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
kamilmysliwiec committed Jan 22, 2025
1 parent 8be48f0 commit 51840e1
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 2 deletions.
2 changes: 2 additions & 0 deletions content/controllers.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions content/middlewares.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions content/migration.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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:

Expand Down

0 comments on commit 51840e1

Please sign in to comment.