Skip to content

Instantly share code, notes, and snippets.

@prateekkathal
Created June 2, 2021 21:31
Show Gist options
  • Save prateekkathal/06bf9af835db87e3662151c1c39371a4 to your computer and use it in GitHub Desktop.
Save prateekkathal/06bf9af835db87e3662151c1c39371a4 to your computer and use it in GitHub Desktop.
For Medium Article (Creating/Writing/Downloading Files in NestJS)
// 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