Creating/Writing/Downloading Files in NestJS
Working on a project, I realized there was no proper way mentioned anywhere on how we can create/write/download file functions in NestJS. So today, I am going to mainly show you the way I approach to it.
If you are new to my articles, please be sure to refer to my other article Best Way to Structure Your Directory/Code (NestJS), which I am going to use in this post as well.
We are going to take example of CSV generation (User exports) for this article.
Note: This article is not about uploading a file. For that, everything is mentioned in the NestJS Docs.
storage.helper.ts
This file contains some helper functions related to reading & writing files using fs
package. You can also put all these in a separate StorageService
but I like to keep them separate & make them globally available for use so that I don’t have to inject them all the time.
users.service.ts
The UsersService
mainly contains all the business logic related to the User
…