Elige cómo responder
Un manejador responde a la petición llamando a uno de los métodos de respuesta de $. Cada uno define el content-type correcto por ti.
| Método | Envía |
|---|---|
$.json(value) | JSON |
$.html(string) | HTML |
$.plain(string) | texto plano |
$.xml(string) | XML |
$.redirect(url, [permanent]) | una redirección (302, o 301 si permanent es true) |
$.empty() | una respuesta vacía (estado 204) |
$.binary(buffer, type) | datos binarios con el 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'));
};