Created
June 2, 2021 21:31
-
-
Save prateekkathal/06bf9af835db87e3662151c1c39371a4 to your computer and use it in GitHub Desktop.
For Medium Article (Creating/Writing/Downloading Files in NestJS)
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
// This file should exist in `src/models/users` | |
import { UsersService } from "./users.service"; | |
import { Response as ExpressResponse } from "express"; | |
import { Controller, Get, Response } from "@nestjs/common"; | |
/** | |
* Controller dealing with user entity based operations. | |
* | |
* @class | |
*/ | |
@Controller("users") | |
export class UsersController { | |
/** | |
* Create an instance of class. | |
* | |
* @constructs | |
* | |
* @param {UsersService} usersService | |
*/ | |
constructor(private readonly usersService: UsersService) {} | |
/** | |
* Exports a CSV & downloads it for users. | |
* | |
* @public | |
* @async | |
* | |
* @returns {Promise<ExpressResponse>} Returns success response entity. | |
*/ | |
@Get("/export-csv") | |
async export(@Response() res: ExpressResponse): Promise<ExpressResponse> { | |
return await this.usersService.exportUserDataToCSV().then( | |
async (fileName) => | |
await this.usersService.getExportedUserCSV(fileName).then((csvData) => { | |
res.set("Content-Type", "text/csv"); | |
return res.send(csvData); | |
}) | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment