Skip to content

Commit 4be0594

Browse files
docs(): some tweaks to the streaming files chapter
1 parent 52f8c45 commit 4be0594

File tree

2 files changed

+12
-11
lines changed

2 files changed

+12
-11
lines changed

content/techniques/streaming-files.md

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
### Streaming Files
1+
### Streaming files
22

3-
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:
3+
> info **Note** This chapter shows how you can stream files from your **HTTP application**. The examples presented below do not apply to GraphQL or Microservice applications.
4+
5+
There may be times where you would like to send back a file from your REST API to the client. To do this with Nest, normally you'd do the following:
46

57
```ts
68
@Controller('file')
@@ -13,20 +15,22 @@ export class FileController {
1315
}
1416
```
1517

16-
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.
18+
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, the framework will take care of piping the response.
1719

18-
#### Streamable File
20+
#### Streamable File class
1921

20-
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.
22+
A `StreamableFile` is a 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.
2123

2224
> info **hint** The `StreamableFile` class can be imported from `@nestjs/common`.
2325
24-
#### Cross Platform Support
26+
#### Cross-platform support
2527

26-
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.
28+
Fastify, by default, can support sending files without needing to call `stream.pipe(res)`, so you don't need to use the `StreamableFile` class at all. 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.
2729

2830
#### Example
2931

32+
You can find a simple example of returning the `package.json` as a file instead of a JSON below, but the idea extends out naturally to images, documents, and any other file type.
33+
3034
```ts
3135
import { Controller, Get, StreamableFile } from '@nestjs/common';
3236
import { createReadStream } from 'fs';
@@ -41,6 +45,3 @@ export class FileController {
4145
}
4246
}
4347
```
44-
45-
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.
46-

src/app/homepage/menu/menu.component.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ export class MenuComponent implements OnInit {
9595
{ title: 'Events', path: '/techniques/events' },
9696
{ title: 'Compression', path: '/techniques/compression' },
9797
{ title: 'File upload', path: '/techniques/file-upload' },
98-
{ title: 'Streaming Files', path: '/techniques/streaming-files' },
98+
{ title: 'Streaming files', path: '/techniques/streaming-files' },
9999
{ title: 'HTTP module', path: '/techniques/http-module' },
100100
{ title: 'Session', path: '/techniques/session' },
101101
{ title: 'Model-View-Controller', path: '/techniques/mvc' },

0 commit comments

Comments
 (0)