# Restrict a route with flags

> Add @flags such as @json, @xml or @upload so a route only matches the requests it is meant for.

Framework: total5
Product: Total.js 5 (total5@0.0.19)
Language: en
Content-SHA256: 2982c373754e84082ffea5091f58ccf444ef6779ee7247e06bc3d914126b8b7c
Snapshot: c0700e321579725c5cf4f5ace1ad7d2866d1b4f93c24a63c0b41b6b7998e976c
Sources: total5-installed@total5@0.0.19
Review: not-recorded

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

```js 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.
