-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1700 from jmcdo29/docs/send-file
docs(streams): adds docs for streaming files with `StreamableFiles`
- Loading branch information
Showing
4 changed files
with
64 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
### Streaming Files | ||
|
||
There won't always be a time where you're sending back JSON or string responses. There may be times where you'd like to send back a file from the server to the client. To do this with Nest, normally you'd do te following: | ||
|
||
```ts | ||
@Controller('file') | ||
export class FileController { | ||
@Get() | ||
getFile(@Res() res: Response) { | ||
const file = createReadStream(join(process.cwd(), 'package.json')); | ||
file.pipe(res); | ||
} | ||
} | ||
``` | ||
|
||
to manage sending the file back. This is still doable in Nest, by injecting `@Res()` in the controller, but in doing so you end up losing access to your post-controller interceptor logic. To handle this, you can return a `StreamableFile` instance and under the hood Nest will take care of piping the response. | ||
|
||
#### Streamable File | ||
|
||
A `StreamableFile` is as class that holds onto the stream that is to be returned. To create a new `StreamableFile`, you can pass either a `Buffer` or a `Stream` to the `StreamableFile` constructor. | ||
|
||
> info **hint** The `StreamableFile` class can be imported from `@nestjs/common`. | ||
#### Cross Platform Support | ||
|
||
Fastify, by default, can support sending files without needing to `stream.pipe(res)`, so you don't need to use the `StreamableFile` class. However, Nest supports the use of `StreamableFile` in both platform types, so if you end up switching between Express and Fastify there's no need to worry about compatibility between the two engines. | ||
|
||
#### Example | ||
|
||
```ts | ||
import { Controller, Get, StreamableFile } from '@nestjs/common'; | ||
import { createReadStream } from 'fs'; | ||
import { join } from 'path'; | ||
|
||
@Controller('file') | ||
export class FileController { | ||
@Get() | ||
getFile(): StreamableFile { | ||
const file = createReadStream(join(process.cwd(), 'package.json')); | ||
return new StreamableFile(file); | ||
} | ||
} | ||
``` | ||
|
||
This is of course a simple example of returning the `package.json` as a file instead of a JSON, but the idea extends out naturally to images, documents, and any other file type. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
src/app/homepage/pages/techniques/streaming-files/streaming-files.component.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
|
||
import { ChangeDetectionStrategy, Component } from '@angular/core'; | ||
import { BasePageComponent } from '../../page/page.component'; | ||
|
||
@Component({ | ||
selector: 'app-streaming-files', | ||
templateUrl: './streaming-files.component.html', | ||
changeDetection: ChangeDetectionStrategy.OnPush, | ||
}) | ||
export class StreamingFilesComponent extends BasePageComponent {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters