Spanish Total.js
English
Documentation For AI agents
Contents

Routing

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

.md GitHub
On this page

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

API

method

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

Declares a route: a page, a REST endpoint, an API operation, a file handler or a WebSocket.
ParameterTypeDescription
url String Method, URL, schema operations, roles, middleware and flags in one line. An array of URLs declares the same route several times.
action? Function|String 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 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 Maximum size of the request body in kilobytes (default_request_maxlength in the config). Default: 10

Returns Object The route that was created.

The first argument describes the whole route in one line:

[+|-][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.

Pages and REST endpoints
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 });
}
API routes
ROUTE('API /api/ -users_list      *Users --> list');
ROUTE('API /api/ -users_read/{id} *Users --> read');
ROUTE('API /api/ +users_save      *Users --> save');
Upload with a timeout and a bigger body
// 60 seconds, 5 MB
ROUTE('POST /upload/', upload, ['upload', 60000], 1024 * 5);
method

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

Adds the same flags, and a URL prefix, to every route declared inside a function.
ParameterTypeDescription
flags? String|Array Flags for every route of the group.
prefix? String A URL prefix, like '/api/'.
declaration Function 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.

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