Total.js ForgeTotal.js 5
English

Total.js 5

Respond with any content type

Total.js 5 · total5@0.0.20 controllersresponses

$.json(), $.html(), $.plain() and $.xml() each set their content type. For anything else, 0.0.20 adds $.content(value, contentType): it sends value (a string or a Buffer) with the content type you pass, and application/octet-stream when you pass none. Use it for a CSV, an ICS calendar, a vCard, an RSS feed, a Markdown file, or bytes that you built in memory.

controllers/content.js
exports.install = function() {
	ROUTE('GET /report/', csv);
	ROUTE('GET /agenda/', ics);
	ROUTE('GET /bytes/', bytes);
};

function csv($) {
	$.content('id,name\n1,Ann\n2,Peter\n', 'text/csv');
}

function ics($) {
	$.content('BEGIN:VCALENDAR\nVERSION:2.0\nEND:VCALENDAR\n', 'text/calendar');
}

function bytes($) {
	$.content(Buffer.from([1, 2, 3]));
}

This only sets the content type and the body. For a download name or for a file on the disk use $.file(), $.binary() or $.stream().