Choose how to respond
A handler answers the request by calling one of the response methods of $. Each one sets the right content-type for you.
| Method | Sends |
|---|---|
$.json(value) | JSON |
$.html(string) | HTML |
$.plain(string) | plain text |
$.xml(string) | XML |
$.redirect(url, [permanent]) | a redirect (302, or 301 when permanent is true) |
$.empty() | an empty response (status 204) |
$.binary(buffer, type) | binary data with the given content type |
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'));
};