Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added path parameters examples to URL BUILDING section #3989

Merged
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
49 changes: 44 additions & 5 deletions docs/Reference/Routes.md
Original file line number Diff line number Diff line change
Expand Up @@ -227,8 +227,23 @@ checked before parametric and wildcard.*

```js
// parametric
fastify.get('/example/:userId', (request, reply) => {})
fastify.get('/example/:userId/:secretToken', (request, reply) => {})
fastify.get('/example/:userId', (request, reply) => {
jsumners marked this conversation as resolved.
Show resolved Hide resolved
/**
* curl ${app-url}/example/12345
* userId === '12345'
*/
jsumners marked this conversation as resolved.
Show resolved Hide resolved
const { userId } = request.params;
/* your code here */
})
fastify.get('/example/:userId/:secretToken', (request, reply) => {
/**
* curl ${app-url}/example/12345/abc.zHi
* userId === '12345'
* secretToken === 'abc.zHi'
*/
const { userId, secretToken } = request.params;
/* your code here */
})

// wildcard
fastify.get('/example/*', (request, reply) => {})
Expand All @@ -237,19 +252,43 @@ fastify.get('/example/*', (request, reply) => {})
Regular expression routes are supported as well, but be aware that you have to escape slashes. Take note that RegExp is also very expensive in terms of performance!
```js
// parametric with regexp
fastify.get('/example/:file(^\\d+).png', (request, reply) => {})
fastify.get('/example/:file(^\\d+).png', (request, reply) => {
/**
* curl ${app-url}/example/12345.png
* file === '12345'
*/
const { file } = request.params;
/* your code here */
})
```

It is possible to define more than one parameter within the same couple of slash
("/"). Such as:
```js
fastify.get('/example/near/:lat-:lng/radius/:r', (request, reply) => {})
fastify.get('/example/near/:lat-:lng/radius/:r', (request, reply) => {
/**
* curl ${app-url}/example/near/15°N-30°E/radius/20
* lat === "15°N"
* lng === "30°E"
* r ==="20"
*/
const { lat, lng, r } = request.params;
/* your code here */
})
```
*Remember in this case to use the dash ("-") as parameters separator.*

Finally, it is possible to have multiple parameters with RegExp:
```js
fastify.get('/example/at/:hour(^\\d{2})h:minute(^\\d{2})m', (request, reply) => {})
fastify.get('/example/at/:hour(^\\d{2})h:minute(^\\d{2})m', (request, reply) => {
/**
* curl ${app-url}/example/at/08h24m
* hour === "08"
* minute === "24"
*/
const { hour, minute } = request.params;
/* your code here */
})
```
In this case as parameter separator it is possible to use whatever character is
not matched by the regular expression.
Expand Down