Escolher como responder
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 |
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'));
};