|
1 |
| -<h3 id='app.param'>app.param([name], callback)</h3> |
| 1 | +<h3 id='app.param'>app.param(name, callback)</h3> |
2 | 2 |
|
3 | 3 | Add callback triggers to [route parameters](/{{ page.lang }}/guide/routing.html#route-parameters), where `name` is the name of the parameter or an array of them, and `callback` is the callback function. The parameters of the callback function are the request object, the response object, the next middleware, the value of the parameter and the name of the parameter, in that order.
|
4 | 4 |
|
@@ -76,79 +76,3 @@ CALLED ONLY ONCE with 3
|
76 | 76 | although this matches
|
77 | 77 | and this matches too
|
78 | 78 | ```
|
79 |
| - |
80 |
| -<div class="doc-box doc-warn" markdown="1"> |
81 |
| -The following section describes `app.param(callback)`, which is deprecated as of v4.11.0. |
82 |
| -</div> |
83 |
| - |
84 |
| -The behavior of the `app.param(name, callback)` method can be altered entirely by passing only a function to `app.param()`. This function is a custom implementation of how `app.param(name, callback)` should behave - it accepts two parameters and must return a middleware. |
85 |
| - |
86 |
| -The first parameter of this function is the name of the URL parameter that should be captured, the second parameter can be any JavaScript object which might be used for returning the middleware implementation. |
87 |
| - |
88 |
| -The middleware returned by the function decides the behavior of what happens when a URL parameter is captured. |
89 |
| - |
90 |
| -In this example, the `app.param(name, callback)` signature is modified to `app.param(name, accessId)`. Instead of accepting a name and a callback, `app.param()` will now accept a name and a number. |
91 |
| - |
92 |
| -```js |
93 |
| -const express = require('express') |
94 |
| -const app = express() |
95 |
| - |
96 |
| -// customizing the behavior of app.param() |
97 |
| -app.param(function (param, option) { |
98 |
| - return function (req, res, next, val) { |
99 |
| - if (val === option) { |
100 |
| - next() |
101 |
| - } else { |
102 |
| - next('route') |
103 |
| - } |
104 |
| - } |
105 |
| -}) |
106 |
| - |
107 |
| -// using the customized app.param() |
108 |
| -app.param('id', 1337) |
109 |
| - |
110 |
| -// route to trigger the capture |
111 |
| -app.get('/user/:id', function (req, res) { |
112 |
| - res.send('OK') |
113 |
| -}) |
114 |
| - |
115 |
| -app.listen(3000, function () { |
116 |
| - console.log('Ready') |
117 |
| -}) |
118 |
| -``` |
119 |
| - |
120 |
| -In this example, the `app.param(name, callback)` signature remains the same, but instead of a middleware callback, a custom data type checking function has been defined to validate the data type of the user id. |
121 |
| - |
122 |
| -```js |
123 |
| -app.param(function (param, validator) { |
124 |
| - return function (req, res, next, val) { |
125 |
| - if (validator(val)) { |
126 |
| - next() |
127 |
| - } else { |
128 |
| - next('route') |
129 |
| - } |
130 |
| - } |
131 |
| -}) |
132 |
| - |
133 |
| -app.param('id', function (candidate) { |
134 |
| - return !isNaN(parseFloat(candidate)) && isFinite(candidate) |
135 |
| -}) |
136 |
| -``` |
137 |
| - |
138 |
| -<div class="doc-box doc-info" markdown="1"> |
139 |
| -The '`.`' character can't be used to capture a character in your capturing regexp. For example you can't use `'/user-.+/'` to capture `'users-gami'`, use `[\\s\\S]` or `[\\w\\W]` instead (as in `'/user-[\\s\\S]+/'`. |
140 |
| - |
141 |
| -Examples: |
142 |
| - |
143 |
| -<pre><code class="language-js"> |
144 |
| -//captures '1-a_6' but not '543-azser-sder' |
145 |
| -router.get('/[0-9]+-[[\\w]]*', function); |
146 |
| - |
147 |
| -//captures '1-a_6' and '543-az(ser"-sder' but not '5-a s' |
148 |
| -router.get('/[0-9]+-[[\\S]]*', function); |
149 |
| - |
150 |
| -//captures all (equivalent to '.*') |
151 |
| -router.get('[[\\s\\S]]*', function); |
152 |
| -</code></pre> |
153 |
| - |
154 |
| -</div> |
0 commit comments