From 8a49e382858139330b4b4fd26843b6d6d90bb563 Mon Sep 17 00:00:00 2001 From: TheThing Date: Thu, 28 Sep 2023 10:39:12 +0000 Subject: [PATCH] Update 'README.md' --- README.md | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 36c4d9d..cbf9126 100644 --- a/README.md +++ b/README.md @@ -126,9 +126,9 @@ flaska.get('/api/test', function(ctx) { }) ``` -### File stream/pipe +### pipe -In cases where the response body is a pipe object (detected from the existance of `.pipe` property), flaska will automatically pipe the file for you. In addition, if a file stream is used, it will read the extension of the file being streamed and automatically fill in the mime-type for you in the `Content-Type` header. +In cases where the response body is a pipe object (detected from the existance of `.pipe` property), flaska will automatically pipe it for you. In addition, if a file stream is used, it will read the extension of the file being streamed and automatically fill in the mime-type for you in the `Content-Type` header. ``` flaska.get('/test.png', function(ctx) { @@ -138,6 +138,22 @@ flaska.get('/test.png', function(ctx) { Flaska will automatically close the file stream for you so you don't have to worry about that. +### FileResponse + +Alternatively, if you want proper file support, I recommend using FileResponse object: + +``` +import { FileResponse } from '../flaska.mjs' + +flaska.get('/test.txt', function(ctx) { + return fs.stat('./test/test.txt').then(function(stat) { + ctx.body = new FileResponse('./test/test.txt', stat) + }) +}) +``` + +This performs a real file stream support, uses pipes and supports all the HTTP shenanigans when it comes to dealing with files, including sending proper etag header, supporting partial response and lots of other things. This is one of the few libraries that actually implements full HTTP partial and etag support in a proper way, almost all other have one or two quirks that don't follow the spec properly. + ### String In other instances, Flaska will `.toString()` the body and send it in response with the specified type or default to `text/plain` if unspecified.