Restrict a route with flags
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:
@jsonand@xml: the body must be JSON or XML@xhr: the request must be an AJAX request (x-requested-withheader)@upload: the body must bemultipart/form-data@debugand@release: the route exists only in that mode@mobileand@robot: the client is a mobile browser or a crawler
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.