You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Parameters defined in [optional segments](/guide/routing/#optional-segments) that are not present in the request URL are omitted from `req.params` entirely.
429
+
428
430
When you use a regular expression for the route definition, capture groups are provided as integer keys using `req.params[n]`, where `n` is the n<sup>th</sup> capture group.
Route paths, in combination with a request method, define the endpoints at which requests can be made. Route paths can be strings, string patterns, or regular expressions.
111
+
Route paths, in combination with a request method, define the endpoints at which requests can be made. Route paths can be strings, string patterns, or regular expressions. They can also capture values from the URL, as described in [Route parameters](#route-parameters) below.
A wildcard (`*`) on its own matches anything at its position, including entire subpaths. For example, this route path will match `/file/style.css` as well as `/file/javascripts/jquery.js`. Wildcards are unnamed, so the matched value is available as `req.params[0]` instead of a named parameter.
@@ -348,6 +368,14 @@ characters with an additional backslash, for example `\\d+`.
348
368
The [`*`](https://github.com/expressjs/express/issues/2495) character in regular expressions is not interpreted in the usual way. As a workaround, use `{0,}` instead of `*`.
349
369
</Alert>
350
370
371
+
Unlike named route parameters, wildcard (`*`) matches in [string patterns](#route-paths-based-on-string-patterns) and capture groups in regular expressions are unnamed: their values are available by position, as `req.params[0]`, `req.params[1]`, and so on.
You can provide multiple callback functions that behave like [middleware](/guide/using-middleware) to handle a request. The only exception is that these callbacks might invoke `next('route')` to bypass the remaining route callbacks. You can use this mechanism to impose pre-conditions on a route, then pass control to subsequent routes if there's no reason to proceed with the current route.
@@ -544,7 +572,7 @@ The methods on the response object (`res`) in the following table can send a res
544
572
## app.route()
545
573
546
574
You can create chainable route handlers for a route path by using `app.route()`.
547
-
Because the path is specified at a single location, creating modular routes is helpful, as is reducing redundancy and typos. For more information about routes, see:[Router() documentation](/api/router).
575
+
Because the path is specified in a single location, this helps to create modular routes and reduces redundancy and typos. For more information about routes, see the[Router() documentation](/api/router).
548
576
549
577
Here is an example of chained route handlers that are defined by using `app.route()`.
550
578
@@ -677,7 +705,7 @@ app.use('/birds', birds);
677
705
678
706
The app will now be able to handle requests to `/birds` and `/birds/about`, as well as call the `timeLog` middleware function that is specific to the route.
679
707
680
-
But if the parent route `/birds` has path parameters, it will not be accessible by default from the sub-routes. To make it accessible, you will need to pass the `mergeParams` option to the Router constructor[reference](/api/application#appuse).
708
+
But if the parent route `/birds` has path parameters, it will not be accessible by default from the sub-routes. To make it accessible, you will need to pass the `mergeParams` option to the [Router constructor](/api/express/#expressrouter).
Route paths, in combination with a request method, define the endpoints at which requests can be made. Route paths can be strings or regular expressions.
111
+
Route paths, in combination with a request method, define the endpoints at which requests can be made. Route paths can be strings or regular expressions. They can also capture values from the URL, as described in [Route parameters](#route-parameters) below.
The characters `?`, `+`, `*`, `[]`, and `()`are reserved and cannot be used as literal characters in route paths. Use `\` to escape them if needed.
158
+
The characters `?`, `+`, `*`, `[]`, `()`, and `!`are reserved and cannot be used as literal characters in route paths, and braces are reserved for [optional segments](#optional-segments). Use `\` to escape them if needed.
Route parameters are named URL segments that are used to capture the values specified at their position in the URL. The captured values are populated in the `req.params` object, with the name of the route parameter specified in the path as their respective keys.
194
+
Route parameters are named URL segments that are used to capture the values specified at their position in the URL. The captured values are populated in the `req.params` object, with the name of the route parameter specified in the path as their respective keys. They come in three forms: [named parameters](#named-parameters) (`:name`), [wildcards](#wildcards) (`*name`), and [optional segments](#optional-segments), which wrap either of them in braces.
195
+
196
+
### Named parameters
197
+
198
+
Named parameters capture a single path segment at their position in the URL, or part of one when combined with literal characters, as shown further below.
Regexp characters are not supported in route paths. Use an array of paths or regular expressions instead.
252
+
Regexp characters are not supported inside string paths, so a parameter cannot be restricted with a suffix such as `:userId(\d+)`. Use an array of paths or a full regular expression instead.
317
253
See the [path route matching syntax](/guide/migrating-5#path-route-matching-syntax) for more information.
318
254
319
255
</Alert>
320
256
257
+
### Wildcards
258
+
259
+
Wildcards match any path after a prefix. Like other route parameters they must have a name, but they are captured as an array of path segments instead of a string.
The braces can also wrap a whole parameter to make it optional. Note that everything inside the braces is optional, so the position of the slash matters:
326
+
327
+
```js
328
+
app.get('/user/{:id}', (req, res) => {
329
+
// GET /user/42 => req.params = { id: '42' }
330
+
// GET /user/ => req.params = {}
331
+
// GET /user => 404, only the parameter is optional
332
+
res.send('ok');
333
+
});
334
+
335
+
app.get('/order{/:id}', (req, res) => {
336
+
// GET /order/42 => req.params = { id: '42' }
337
+
// GET /order => req.params = {}, the whole segment is optional
// GET /order => req.params = {}, the whole segment is optional
355
+
res.send('ok');
356
+
});
357
+
```
358
+
359
+
Do not confuse the position of the slash in the route path with the [`strict routing` setting](/api/application/#application-settings), which is about the request URL: it controls whether a URL ending in a slash that the route path does not require still matches. For example, a request for `/order/` matches the `/order{/:id}` route by default, but returns a 404 error when strict routing is enabled; the trailing slash of `/user/` is unaffected because the `/user/{:id}` route requires it. All the requests commented in the examples above behave the same regardless of that setting.
360
+
321
361
## Route handlers
322
362
323
363
You can provide multiple callback functions that behave like [middleware](/guide/using-middleware) to handle a request. The only exception is that these callbacks might invoke `next('route')` to bypass the remaining route callbacks. You can use this mechanism to impose pre-conditions on a route, then pass control to subsequent routes if there's no reason to proceed with the current route.
@@ -514,7 +554,7 @@ The methods on the response object (`res`) in the following table can send a res
514
554
## app.route()
515
555
516
556
You can create chainable route handlers for a route path by using `app.route()`.
517
-
Because the path is specified at a single location, creating modular routes is helpful, as is reducing redundancy and typos. For more information about routes, see:[Router() documentation](/api/router).
557
+
Because the path is specified in a single location, this helps to create modular routes and reduces redundancy and typos. For more information about routes, see the[Router() documentation](/api/router).
518
558
519
559
Here is an example of chained route handlers that are defined by using `app.route()`.
520
560
@@ -647,7 +687,7 @@ app.use('/birds', birds);
647
687
648
688
The app will now be able to handle requests to `/birds` and `/birds/about`, as well as call the `timeLog` middleware function that is specific to the route.
649
689
650
-
But if the parent route `/birds` has path parameters, it will not be accessible by default from the sub-routes. To make it accessible, you will need to pass the `mergeParams` option to the Router constructor[reference](/api/application#appuse).
690
+
But if the parent route `/birds` has path parameters, it will not be accessible by default from the sub-routes. To make it accessible, you will need to pass the `mergeParams` option to the [Router constructor](/api/express/#expressrouter).
0 commit comments