---
title: Routing
project: Total.js V4
version: 0.0.99
section: reference
language: en
license: MIT
url: https://totaljs.es/docs/total-js-v4/0-0-99/reference/routing/
updated: 2026-11-16T15:41:00.817Z
---

# Routing

> The ROUTE() function: every way to declare a route in Total.js 4.

Routes are declared with `ROUTE()`, usually in `exports.install` of a controller. The guide [Routing](../../guides/routing/) explains each part with examples; this page is the exact reference.

## API

### ROUTE(url, [action], [flags], [length])

Type: `method`

Declares a route: a page, a REST endpoint, an API operation, a file handler or a WebSocket.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `url` | `String` | yes |  | Method, URL, schema operations, roles, middleware and flags in one line. An array of URLs declares the same route several times. |
| `action` | `Function|String` | no |  | The action, run with the controller as `this` and the dynamic parameters as arguments; or the name of a view. Without it, the route renders the view named after the URL, or runs the schema operations. |
| `flags` | `Array` | no |  | Flags (`json`, `upload`, `cors`, `csrf`, `referer`, `mobile`, `robot`, `encrypt`, `delay`, `novalidate`, `xml`, `raw`, origins, `credentials`, `#middleware`, `@role`, `// description`, `id:name`), a number with the timeout in milliseconds, or an object with options. A number here is taken as the length. |
| `length` | `Number` | no | `10` | Maximum size of the request body in kilobytes (`default_request_maxlength` in the config). |

Returns: `Object` The route that was created.

The first argument describes the whole route in one line:

```text
[+|-][METHOD] /url/{param}/ [*Schema --> operations] [@role] [#middleware] [flags]
```

- `+` only signed-in requests, `-` only the others; a `@role` implies `+`.
- `METHOD`: `GET` (default), `POST`, `PUT`, `PATCH`, `DELETE`, `HEAD`, `OPTIONS`, several separated by commas, or `API` for an API route.
- `{param}`: a dynamic part, optionally typed: `{id:number}`, `{active:boolean}`, `{date:date}`, `{id:uid}`, `{id:guid}`. A `*` at the end matches the rest of the path.
- `*Schema --> operations`: validates the body with the schema and runs its operations; `--> operation` runs an operation without a schema. `(response)` after an operation chooses whose result is returned.
- `[subdomain]` before the URL limits the route to a subdomain.

A URL that starts with `FILE` creates a file route, and one that starts with `SOCKET`, `WS` or `WSS` a WebSocket route. `ROUTE(url, null)` removes the route.

Example: Pages and REST endpoints
```js
exports.install = function() {
	ROUTE('GET /', home);
	ROUTE('GET /users/{id}/', user);
	ROUTE('+POST /api/users/ *Users --> insert');
	ROUTE('+GET /admin/ @admin', admin);
};

function user(id) {
	this.json({ id: id });
}
```

Example: API routes
```js
ROUTE('API /api/ -users_list      *Users --> list');
ROUTE('API /api/ -users_read/{id} *Users --> read');
ROUTE('API /api/ +users_save      *Users --> save');
```

Example: Upload with a timeout and a bigger body
```js
// 60 seconds, 5 MB
ROUTE('POST /upload/', upload, ['upload', 60000], 1024 * 5);
```

### GROUP([flags], [prefix], declaration)

Type: `method`

Adds the same flags, and a URL prefix, to every route declared inside a function.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `flags` | `String|Array` | no |  | Flags for every route of the group. |
| `prefix` | `String` | no |  | A URL prefix, like `'/api/'`. |
| `declaration` | `Function` | yes |  | The function that declares the routes. |

Returns: `undefined` Nothing.

The arguments can come in any order. An array, or a string without `/` (`'authorize,json'`), is a list of flags. A string with `/` is a prefix; a trailing slash is removed. The function is called right away, with the framework as `this`.

The flags are added to every `ROUTE()`, `WEBSOCKET()` and `FILE()` inside the function, without duplicates. The prefix is added only to the URLs of `ROUTE()` HTTP routes, not to `ROUTE('SOCKET ...')`, `ROUTE('FILE ...')`, `WEBSOCKET()` or `FILE()`.

Groups do not nest: the end of an inner `GROUP()` clears the flags and the prefix of the outer one.

Example: Prefix and flags
```js
exports.install = function() {
	GROUP('/api/', ['authorize'], function() {
		ROUTE('GET  /users/     *Users --> query');   // GET /api/users/
		ROUTE('POST /users/     *Users --> insert');  // POST /api/users/
	});
};
```
