Skip to content

This API allows users to upload, download, rename, and delete files in MongoDB using GridFS. It supports single and multiple file uploads, as well as various download formats, including zipped files and base64 encoding.

License

Notifications You must be signed in to change notification settings

Prokken/file-upload-with-mongodb

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Using GridFS for storing files into MongoDB

This code sets up an Express server for handling file uploads, downloads, renaming, and deletions using MongoDB's GridFS for storage. Here's a detailed breakdown of what each part of the code does:

1. Imports and Configuration

  • express: A web framework for Node.js used to set up the server and define routes.
  • bodyParser: Middleware for parsing JSON request bodies.
  • logger (morgan): Middleware for logging HTTP requests.
  • dotenv: Loads environment variables from a .env file into process.env.
  • connectDB: A custom module that handles the connection to the database.
  • mongoose: An ODM (Object Data Modeling) library for MongoDB and Node.js, allowing interaction with the MongoDB database.
  • upload: A custom utility from ./utils/upload that handles file uploads.
  • archiver: A library used for creating ZIP archives.
  • stream: Node.js module to handle streaming data.

Configuration and Setup:

  • dotenv.config(): Loads environment variables from .env file.
  • app = express(): Creates an Express application.
  • connectDB(): Establishes a connection to the MongoDB database.

2. GridFS Bucket Initialization

  • GridFS: MongoDB specification for storing and retrieving large files like images, audio files, etc.
  • bucket: A GridFSBucket instance is created when the MongoDB connection is established. It uses the collection uploads for storing files.

3. Middleware

  • bodyParser.json(): Parses incoming requests with JSON payloads.
  • logger('dev'): Logs requests to the console.

4. Routes for API Endpoints

  • /upload/file:

    • Uploads a single file.
    • Uses upload().single('file') to handle single file uploads.
    • On success, responds with a 201 status and a success message; on failure, responds with a 400 status and an error message.
  • /upload/files:

    • Uploads multiple files.
    • Uses upload().array('files') to handle multiple file uploads.
    • Similar success and error handling as the single file upload.
  • /download/files/:fileId:

    • Downloads a specific file by its ID.
    • Retrieves the file from GridFS, sets appropriate headers, and pipes the file stream to the response.
    • If the file is not found, responds with a 404 status.
  • /download/files-zip:

    • Downloads all files as a ZIP archive.
    • Retrieves all files from GridFS, uses archiver to create a ZIP, and pipes it to the response.
    • If no files are found, responds with a 404 status.
  • /download/files-base64:

    • Downloads all files in Base64 format.
    • Retrieves files, converts each file to Base64, and sends an array of Base64 strings in the response.
  • /rename/file/:fileId:

    • Renames a file identified by its ID.
    • Uses GridFS's rename function and responds with success or error messages.
  • /delete/file/:fileId:

    • Deletes a file by its ID.
    • Uses GridFS's delete function and responds with appropriate success or error messages.

5. Server Listening

  • app.listen(port, ...):
    • Starts the server and listens on the specified port (default 3000 if not defined in the environment variables).
    • Logs a message indicating the server is running.

Key Points:

  • Error Handling: Each route includes try-catch blocks for handling errors and responding with relevant status codes and messages.
  • Streaming: Uses streams extensively for efficient handling of file uploads and downloads.
  • Mongoose Integration: Relies on Mongoose for interacting with MongoDB, especially for file management with GridFS.
  • Archiver Usage: Utilizes the archiver package to compress files into ZIP format for batch downloads.

This setup provides a comprehensive file management system using Node.js, Express, and MongoDB, facilitating file operations via RESTful API endpoints.

or, Checkout this video demonstration

file-upload-with-mongodb.mp4

About

This API allows users to upload, download, rename, and delete files in MongoDB using GridFS. It supports single and multiple file uploads, as well as various download formats, including zipped files and base64 encoding.

Topics

Resources

License

Stars

Watchers

Forks