Skip to content

Commit e26a215

Browse files
committed
docs: change express sample to Nest sample
1 parent 522a740 commit e26a215

File tree

1 file changed

+9
-4
lines changed

1 file changed

+9
-4
lines changed

content/techniques/streaming-files.md

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
### 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. In an express application, you may use something like
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:
44

55
```ts
6-
app.get('send-file', (req, res) => {
7-
createReadStream(path(process.cwd(), 'package.json')).pipe(res);
8-
});
6+
@Controller('file')
7+
export class FileController {
8+
@Get()
9+
getFile(@Res() res: Response) {
10+
const file = createReadStream(join(process.cwd(), 'package.json'));
11+
file.pipe(res);
12+
}
13+
}
914
```
1015

1116
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.

0 commit comments

Comments
 (0)