Search code examples
node.jsmongodbexpressnodesfile-management

How should I store files in NodeJS


  • I'm a newer in Nodejs and researching on how to upload file using Multer in NodeJS. And I saw that most of the tutorial out there store all kinds of files (audio, avatar photo, banner photo, text, PDFs,..) in only a static "public" folder.
  • My first question: is that a best way to store file in backend? Should I create a subfolder for a specific user to store all that user's files (ex: User1Folder, User2Folder) or for a type of files (ex: PDFFolder, AvatarFolder,..).
  • My second question: should I create a File model like this: File{ id: "", name: "", uploadedDate: "", ... } then save it in database.

Solution

  • Its common for companies to opt for 3rd party's or separate storage service that optimized for file storage. Some comes with free-trial-ish plan like aws-s3, Google's gcs, azure, and bunch others.

    But if it really comes to a file storage in our own service, it's good enough to put all the files in the single same folder (flat file directory) and do the file categorization by attaching the file to the corresponding models. Putting something like this in the data: { "username": "arege", "name": "Arle Greg", "profile_picture_file":"timestamp_somehash.png"}

    For the second question, if your service need to record dates for all the uploaded file types in general, then its good to have that kind of model. But if its only for a little part of the file types in the service, it can just be written in the corresponding model along with the file link.