Spanish Total.js
English
Documentation For AI agents
Contents

Middleware

NEWMIDDLEWARE(), the middleware handler, the #name route flag and the middleware option of actions.

.md GitHub
On this page

API

method

NEWMIDDLEWARE(name, fn, [assign])

Registers a named middleware, or removes it.
ParameterTypeDescription
name String Name of the middleware, used as #name in routes. Letters, digits and _.
fn? Function function($, next). Without it, the middleware is removed.
assign? String|Array routes, dynamic or websockets, separated by commas.

Returns undefined

The function runs as fn($, next) for every route that lists the name. For HTTP and socket routes $ is the controller; for actions it is the action's $. Call next() to continue, or answer the request to stop it.

assign adds the middleware to routes that already exist: 'routes' or 'dynamic' for HTTP routes, 'websockets' for socket routes, several values separated by commas or as an array. Routes registered later do not get it.

Called with only name, it removes the middleware from the registry and from every route.

API key check
NEWMIDDLEWARE('apikey', function($, next) {
	if ($.headers['x-api-key'] === CONF.apikey)
		next();
	else
		$.invalid(401);
});

ROUTE('GET /api/orders/ #apikey', function($) {
	$.json([]);
});
Remove a middleware
NEWMIDDLEWARE('apikey');
method

MIDDLEWARE(name, fn, [assign])

The same function as NEWMIDDLEWARE().
ParameterTypeDescription
name String Name of the middleware.
fn? Function function($, next). Without it, the middleware is removed.
assign? String|Array routes, dynamic or websockets.

Returns undefined

MIDDLEWARE and NEWMIDDLEWARE are two names for one function; the arguments and the behaviour are the same.

Register
MIDDLEWARE('timing', function($, next) {
	$.started = Date.now();
	next();
});
delegate

function($, next)

The function of a middleware.
ParameterTypeDescription
$ Controller|Options The controller of the request, or the action's $.
next Function Continues with the next middleware or the action.

It receives the controller (or, inside an action, the action's $) and the next callback. Calling next() runs the next middleware of the route, and after the last one the action. Not calling next() stops the chain; then the middleware must answer the request itself, for example with $.invalid(401), $.redirect(url) or $.json(data).

For HTTP routes, the body, the query, the URL parameters and $.user are already set when the middleware runs.

Redirect anonymous users
NEWMIDDLEWARE('login', function($, next) {
	if ($.user)
		next();
	else
		$.redirect('/login/');
});
property

ROUTE('METHOD /url/ #name', action)

Lists a middleware in a route declaration.

Each #name in the route string adds that middleware to the route; they run in the written order. The flag works for HTTP routes, API routes and SOCKET routes. The name may contain letters, digits and _. A name that is not registered is skipped; in a SOCKET route, the middleware after it are skipped too.

Two middleware
ROUTE('POST /api/upload/ #apikey #ratelimit', upload);
ROUTE('SOCKET /live/ #apikey', live);
property

NEWACTION({ middleware: 'name1, name2' })

Middleware that runs before an action.

The middleware option of NEWACTION() is a list of middleware names separated by commas, spaces or |. They run with the action's $ every time the action is called, before the action function. A name that is not registered is reported with Total.error() and skipped.

Action with middleware
NEWACTION({
	name: 'Orders/list',
	middleware: 'tenant',
	action: function($) {
		$.callback([]);
	}
});
type

middleware/*.js

Folder with files that register middleware.

.js files in middleware/ and in plugins/<name>/middleware/ are loaded at start; their install() function runs. Use it to keep middleware apart from controllers.

middleware/auth.js
exports.install = function() {
	NEWMIDDLEWARE('admin', function($, next) {
		if ($.user && $.user.sa)
			next();
		else
			$.invalid(401);
	});
};
Made in Europe