ForgeTotal.js 5
en

Total.js 5

Restrict a route with flags

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

Flags start with @ and go after the URL. A route only matches when the request satisfies all of its flags; otherwise Total.js tries the next route for the same URL, and answers 404 when none matches. Useful flags:

  • @json and @xml: the body must be JSON or XML
  • @xhr: the request must be an AJAX request (x-requested-with header)
  • @upload: the body must be multipart/form-data
  • @debug and @release: the route exists only in that mode
  • @mobile and @robot: the client is a mobile browser or a crawler
controllers/data.js
exports.install = function() {
	ROUTE('POST /data/ @json', json_only);
	ROUTE('POST /data/ @xml', xml_only);

	// Careful: a route with flags has a lower priority than the same URL without flags
	ROUTE('POST /mixed/ @json', json_only);
	ROUTE('POST /mixed/', anything);
};

function json_only($) {
	$.json({ handler: 'json' });
}

function xml_only($) {
	$.json({ handler: 'xml' });
}

function anything($) {
	$.json({ handler: 'anything' });
}

Never mix a flagged route and an unflagged route on the same URL: the unflagged one is checked first and takes every request, so the flagged route is never reached.