Spanish Total.js
English
Documentation For AI agents
Contents

Routing

Methods, dynamic parameters, signed-in users, schemas, API routes, views and flags of ROUTE().

.md GitHub
On this page

Every route is declared with ROUTE(), usually inside exports.install of a file in controllers/.

exports.install = function() {
    ROUTE('GET /products/', products);
    ROUTE('POST /products/', create);
};

The first argument describes the route in one line: method, URL, and optionally a schema operation, middleware, roles and flags. The full list of arguments is in the reference of ROUTE().

Methods

GET is the default. Write the method before the URL: GET, POST, PUT, PATCH, DELETE, HEAD, OPTIONS. Several methods are separated by commas:

ROUTE('GET,POST /contact/', contact);

Dynamic parameters

Parts in braces are parameters. They arrive as arguments of the action, in their order, and in this.params:

ROUTE('GET /users/{id}/', read);
ROUTE('GET /blog/{year}/{slug}/', article);

function article(year, slug) {
    // this.params.year, this.params.slug
}

A parameter can have a type: {page:number}, {active:boolean} and {date:date} are converted in this.params. {id:uid} and {id:guid} accept only identifiers of that kind.

A * at the end of the URL matches the rest of the path:

ROUTE('GET /docs/*', docs);

Signed-in users

+ before the method allows only requests that your AUTH() delegate accepted, and - only the others. A role (@admin) implies a signed-in user:

ROUTE('+GET /account/', account);
ROUTE('-GET /login/', login);
ROUTE('+GET /admin/ @admin', admin);

Schemas and actions

A route can call operations of a schema without writing an action. *Schema --> operation validates the request body with the schema and runs the operation:

ROUTE('POST /api/users/ *Users --> insert');
ROUTE('GET  /api/users/{id}/ *Users --> read');

Several operations run one after another. (response) after one of them says whose result is sent back:

ROUTE('POST /api/orders/ *Orders --> check insert (response) notify');

API routes

API groups many operations behind one URL. The client sends POST with { "schema": "name/params", "data": {} }; + marks the operations that take data and - those that do not:

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

Views

An action can be the name of a view. Without an action, the view is chosen from the URL:

ROUTE('GET /about/', 'about');   // views/about.html
ROUTE('GET /pricing/');           // views/pricing.html

Flags

Flags change how a route works. Write them in the URL after a space, or pass them as an array:

ROUTE('POST /upload/', upload, ['upload', 60000], 1024 * 5);
  • json, xml, raw: the body comes as JSON, XML or raw data (the route becomes POST).
  • upload: the body is multipart/form-data with files.
  • cors, origins (https://app.com) and credentials: cross-origin requests.
  • csrf, referer: checks the CSRF token or the referer.
  • mobile, robot: only for mobile devices, or only for crawlers.
  • encrypt: the answer is encrypted.
  • delay: the request has no timeout.
  • novalidate: the schema converts the body without validating it.
  • A number: the timeout of the request in milliseconds (default 3000).
  • #name or &name: runs a middleware first.
  • // text: a description of the route.

Request size

The last argument limits the size of the request body, in kilobytes (default 10). It is needed for big JSON bodies and uploads:

ROUTE('POST /api/import/ json', importdata, 1024);   // 1 MB

Files and WebSockets

FILE routes handle static-like files and SOCKET routes open WebSockets; they have their own pages in the reference.

ROUTE('FILE /docs/*.pdf', pdf);
ROUTE('SOCKET /chat/', chat);

Subdomains and removing routes

A route only for a subdomain starts with it in brackets: ROUTE('[api]/users/', users). ROUTE(url, null) removes a route that was declared before.

Made in Europe