This code demonstrates a basic implementation of file management using MongoDB's GridFS with Node.js and Express. It showcases how to upload, retrieve, stream, and download files using GridFS.
Before running the code, make sure you have the following prerequisites installed:
- Node.js
- MongoDB
git clone <repository-url>
: clone the repository or download the code.cd <project-directory>
npm install
Navigate to the project directory in your terminal abd Install the dependencies by running the following command:
- Create a
.env
file in the project root directory and configure the following environment variables:
MONGO_CONNECTION_STRING=<your_mongodb_connection_string>
PORT=<optional_port_number>
Replace <your_mongodb_connection_string>
with the actual connection string for your MongoDB instance. Optionally, you can set the PORT
variable to specify the desired server port (default is 3000).
-
Ensure that your MongoDB instance is running.
-
Run
node <server_file_name>.js
,npm run dev
ornpm start
commands to start the server. Replace<server_file_name>
with the name of the file containing the server configuration (e.g.,server.js
) -
The server will start running and listening for requests on the specified port.
The following endpoints are available for file management:
-
GET /files
: Retrieves a list of all files stored in the GridFS collection. -
GET /file/:filename
: Retrieves a specific file by its filename. -
GET /stream/:filename
:Streams and downloads a file by its filename. -
POST /upload
: Uploads one or multiple files. The files should be sent using thefiles
field in a multipart form-data request. The request should also include theuploadBy
attribute in the body to identify the uploader.
This code is meant for demonstration purposes and may not cover all possible scenarios or edge cases. Contributions, improvements, and suggestions are welcome. If you find any issues or have ideas for enhancements, feel free to open an issue or submit a pull request.