# Escolher como responder

> Envie JSON, HTML, texto simples, XML, redirecionamentos, respostas vazias ou dados binários a partir de um handler de rota.

Framework: total5
Product: Total.js 5 (total5@0.0.19)
Language: pt
Content-SHA256: 20ff4135ad793f4b5ceaf7f10709a56d6c0fdfc25964fb965e15af41b96c904c
Snapshot: c0700e321579725c5cf4f5ace1ad7d2866d1b4f93c24a63c0b41b6b7998e976c
Sources: total5-installed@total5@0.0.19
Review: not-recorded

Um handler responde ao pedido chamando um dos métodos de resposta de `$`. Cada um define o `content-type` certo por si.

| Método | Envia |
|---|---|
| `$.json(value)` | JSON |
| `$.html(string)` | HTML |
| `$.plain(string)` | texto simples |
| `$.xml(string)` | XML |
| `$.redirect(url, [permanent])` | um redirecionamento (302, ou 301 quando `permanent` é true) |
| `$.empty()` | uma resposta vazia (estado 204) |
| `$.binary(buffer, type)` | dados binários com o content type indicado |

```js controllers/responses.js
exports.install = function() {
	ROUTE('GET /json/', ($) => $.json({ ok: true }));
	ROUTE('GET /html/', ($) => $.html('<h1>Hello</h1>'));
	ROUTE('GET /plain/', ($) => $.plain('Hello'));
	ROUTE('GET /xml/', ($) => $.xml('<root><item>1</item></root>'));
	ROUTE('GET /redirect/', ($) => $.redirect('/json/'));
	ROUTE('GET /moved/', ($) => $.redirect('/json/', true));
	ROUTE('GET /empty/', ($) => $.empty());
	ROUTE('GET /binary/', ($) => $.binary(Buffer.from('abc'), 'application/octet-stream'));
};
```
