Total.js ForgeTotal.js 5
Deutsch

Total.js 5

Mit beliebigem Content-Type antworten

Total.js 5 · total5@0.0.20 controllersresponses

$.json(), $.html(), $.plain() und $.xml() setzen jeweils ihren eigenen Content-Type. Für alles andere fügt 0.0.20 $.content(value, contentType) hinzu: Es sendet value (einen String oder einen Buffer) mit dem Content-Type, den du übergibst, und mit application/octet-stream, wenn du keinen angibst. Nutze es für eine CSV-Datei, einen ICS-Kalender, eine vCard, einen RSS-Feed, eine Markdown-Datei oder Bytes, die du im Speicher aufgebaut hast.

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]));
}

Dies setzt nur den Content-Type und den Body. Für einen Download-Namen oder für eine Datei auf der Festplatte verwende $.file(), $.binary() oder $.stream().