ForgeTotal.js 5
en

Total.js 5

Choose how to respond

Total.js 5 · total5@0.0.19 routingresponse Executed against Total.js 5

A handler answers the request by calling one of the response methods of $. Each one sets the right content-type for you.

MethodSends
$.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
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'));
};