Need a Nest Controller: File and Stream Controller
=====================================================
In this lesson, we will learn how to create a customized controller in NestJS that can download files. We will go through an example where we need to download a package.json file from a CRUN project and save it on the user's computer.
### Downloading Files without Extension
When you download a file using the `download` method, it downloads only the text content of the file without including the extension. For example, if we have a package.json file with the following contents:
```json
{
"name": "example",
"version": "1.0.0",
"description": "Example project"
}
```
The downloaded file will be saved as a plain text file without the .json extension.
### Downloading Files with Extension
To download files with their original extensions, we need to use the `stream` method and specify the content type of the file. For example, if we have an image file, we can specify the content type as 'image/jpeg' or 'application/json' for a JSON file.
Here's how you can create a new controller that downloads files:
```javascript
import { Controller, Get, Post, Req, Res } from '@nestjs/common';
import { FileService } from './file.service';
@Controller('files')
export class FilesController {
constructor(private readonly fileService: FileService) {}
@Get(':fileName')
async downloadFile(@Req() req, @Res() res) {
const fileName = req.params.fileName;
const fileExtension = '.json';
const filePath = 'path/to/file';
// Download the file using stream method
const fileStream = this.fileService.readFile(filePath);
// Set the response headers
res.set({
'Content-Disposition': `attachment; filename=${fileName}${fileExtension}`,
'Content-Type': 'application/json',
});
return fileStream;
}
}
```
In this example, we create a new controller with an endpoint `/files/:fileName`. When a request is made to this endpoint, the `downloadFile` method is called. This method downloads the file using the `stream` method and sets the response headers accordingly.
### Downloading Package.json File
To download the package.json file from a CRUN project, we can use the same approach as above. We need to specify the content type as 'application/json' and set the response headers accordingly.
Here's an example of how you can create a new method in the `FilesController` class that downloads the package.json file:
```javascript
import { Controller, Get, Post, Req, Res } from '@nestjs/common';
import { FileService } from './file.service';
@Controller('files')
export class FilesController {
constructor(private readonly fileService: FileService) {}
@Get(':fileName')
async downloadFile(@Req() req, @Res() res) {
const fileName = req.params.fileName;
const fileExtension = '.json';
const filePath = 'path/to/file';
// Download the file using stream method
const fileStream = this.fileService.readFile(filePath);
// Set the response headers
res.set({
'Content-Disposition': `attachment; filename=${fileName}${fileExtension}`,
'Content-Type': 'application/json',
});
return fileStream;
}
@Get('package.json')
async downloadPackageJson(@Req() req, @Res() res) {
const fileName = 'package.json';
const filePath = 'path/to/package.json';
// Download the package.json file using stream method
const fileStream = this.fileService.readFile(filePath);
// Set the response headers
res.set({
'Content-Disposition': `attachment; filename=${fileName}`,
'Content-Type': 'application/json',
});
return fileStream;
}
}
```
In this example, we create a new method called `downloadPackageJson` that downloads the package.json file from a specific path. The response headers are set to indicate that the downloaded file is an attachment with the filename `package.json`.