group()
Signature
Section titled “Signature”app.group(prefix, [...middleware])Parameters
Section titled “Parameters”| Name | Type | Description |
|---|---|---|
prefix | string | URL prefix for the group (e.g. '/admin'). A leading / is added automatically if missing. Trailing slashes are removed. |
...middleware | Function | Zero or more middleware functions applied to every route registered in the group. |
Returns
Section titled “Returns”A KaelumGroup object with the following methods, all of which return the group itself for chaining:
| Method | Description |
|---|---|
addRoute(path, handlers) | Register routes (relative to group prefix) |
apiRoute(resource, handlers) | Register RESTful routes |
redirect(from, to, [status]) | Register a redirect within the group |
healthCheck([pathOrOptions]) | Register a health endpoint within the group |
group(subPrefix, [...middleware]) | Create a nested sub-group |
The returned group also exposes prefix (string) and router (Express Router) as read-only properties.
Examples
Section titled “Examples”// Basic groupconst admin = app.group('/admin');admin.addRoute('/users', { get: listUsers });
// With shared middlewareconst api = app.group('/api/v1', authMiddleware, logMiddleware);api.apiRoute('products', { get: listProducts });
// Nested groupsconst v2 = app.group('/api/v2');const internal = v2.group('/internal', requireInternal);internal.addRoute('/metrics', { get: getMetrics });
// Chainedapp.group('/public') .addRoute('/about', { get: aboutPage }) .addRoute('/contact', { get: contactPage });