ForgeTotal.js 5
de

Total.js 5

Eine Route mit Flags einschränken

Total.js 5 · total5@0.0.19 routingflags Mit Total.js 5 ausgeführt

Flags beginnen mit @ und stehen nach der URL. Eine Route passt nur, wenn die Anfrage alle ihre Flags erfüllt; andernfalls probiert Total.js die nächste Route für dieselbe URL und antwortet mit 404, wenn keine passt. Nützliche Flags:

  • @json und @xml: Der Body muss JSON oder XML sein
  • @xhr: Die Anfrage muss eine AJAX-Anfrage sein (Header x-requested-with)
  • @upload: Der Body muss multipart/form-data sein
  • @debug und @release: Die Route existiert nur in diesem Modus
  • @mobile und @robot: Der Client ist ein mobiler Browser oder ein 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' });
}

Mische nie eine Route mit Flags und eine ohne Flags auf derselben URL: Die ohne Flags wird zuerst geprüft und übernimmt jede Anfrage, die Route mit Flags wird also nie erreicht.