Appearance
param
Retrieves a route, query, file, or form parameter from the request context.
Syntax
js
const value = param('key')
const value = param('key', 'default')
const value = param('key', () => 'defaultFn')Details
param attempts to retrieve a value from the following, in order:
- Route params
- Query string
- POST form data
- Uploaded files (if the key is associated with a file input)
If the key is not found in any source:
- If a default value is provided, it is returned.
- If a default function is provided, it is called and its return value is used instead.
- Otherwise,
undefinedis returned.
Examples
Basic usage
js
// Route: /users/:id
// Request: GET /users/42
const id = param('id') // → '42'With query string
js
// Request: GET /users?id=42&expand=true
const expand = param('expand') // → 'true'With default fallback
js
const page = param('page', '1') // → '1' if not provided
const token = param('token', generateToken)Retrieving uploaded file
js
const avatar = param('profile_picture')
if (avatar) {
// avatar is available here and can be saved to database, etc.
db.query("INSERT INTO avatars SET avt_image=?",avatar);
}Notes
- File parameters are available if the request is
multipart/form-data. - In MARS Engine,
param()unifies access across various request sources to simplify handler logic.