Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/prokken/file-upload-with-mongodb
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.
https://github.com/prokken/file-upload-with-mongodb
gridfs mongodb mongoose
Last synced: 9 days ago
JSON representation
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.
- Host: GitHub
- URL: https://github.com/prokken/file-upload-with-mongodb
- Owner: Prokken
- License: mpl-2.0
- Created: 2024-09-14T20:50:21.000Z (3 months ago)
- Default Branch: main
- Last Pushed: 2024-09-14T21:24:34.000Z (3 months ago)
- Last Synced: 2024-12-18T23:13:26.336Z (9 days ago)
- Topics: gridfs, mongodb, mongoose
- Language: JavaScript
- Homepage:
- Size: 14.6 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# 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
https://github.com/user-attachments/assets/16fd94bb-7095-406d-a9e7-09cb3b1d18b0