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:
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 intoprocess.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.
- 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 collectionuploads
for storing files.
bodyParser.json()
: Parses incoming requests with JSON payloads.logger('dev')
: Logs requests to the console.
-
/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.
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.
- 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.