ForgeTotal.js 5
en

Total.js 5

Add a middleware to a route

Total.js 5 · total5@0.0.19 middlewareroutingsecurity Executed against Total.js 5

A middleware is a function that runs before the route handler. Call next() to continue or answer the request yourself to stop it. Register it once with MIDDLEWARE(name, fn) and attach it to a route with #name.

definitions/middleware.js
MIDDLEWARE('apikey', function($, next) {
	if ($.headers['x-api-key'] === 'abc123')
		next();
	else
		$.invalid(401);
});
controllers/secure.js
exports.install = function() {
	ROUTE('GET /secure/ #apikey', secure);
	ROUTE('GET /open/', open);
};

function secure($) {
	$.json({ ok: true });
}

function open($) {
	$.json({ ok: true });
}