This is a simple implementation of a file storage system using MongoDB's GridFS. It allows you to store files in a MongoDB database and retrieve them later.
To clone this repository, run the following command:
git clone https://github.com/vikashkrdeveloper/MongoGridFS-FilesBucket.git
Then check into the cloned repository:
cd MongoGridFS-FilesBucket
After that, install the required dependencies:
npm install
set the environment variables in a .env
file in the root directory of the project. The following environment variables are required:
MONGO_URI=mongodb://localhost:27017/files-bucket
PORT=8000
And finally, start the server:
npm start
# Output
```bash
$ node ./bin/www
Connected to database: gridfs
Base URL:http://localhost:8000
To upload a file, send a POST request to the /upload/image
endpoint with the file you want to upload. The file should be sent as a form-data request with the key file
and caption
as a string.
curl -X POST -F "file=@/path/to/file.jpg" -F "caption=This is a caption" http://localhost:8000/upload/image
To retrieve a file, send a GET request to the /get-image/:filename
endpoint with the filename of the file you want to retrieve.
The server is running on port 8000. You can now test the API endpoints using Postman or any other API testing tool.
The following are the API endpoints available in the application:
Method | Endpoint | Description |
---|---|---|
POST | /upload/image | Upload an image file |
GET | /get-image/:filename | Retrieve an image file |
GET | /get-all-images | Retrieve all images |
POST | /image/delete/:id | Delete an image |
GET | /image/recent | Fetch most recent image |
POST | /multiple/image/upload | Upload multiple files |
GET | /get-image/:filename | Fetch a particular image and render on browser |
DELETE | /file/del/:id | Delete a particular file by an ID |
- Node.js - JavaScript runtime
- Express - Web framework for Node.js
- MongoDB - NoSQL database
- GridFS - MongoDB's file storage system
- Multer - Middleware for handling
multipart/form-data
- Multer GridFS Storage - Multer storage engine for GridFS
- Mongoose - MongoDB object modeling tool
- Dotenv - Module that loads environment variables from a
.env
file intoprocess.env
- Cors - Middleware for enabling CORS with various options
- Body-parser - Middleware for parsing incoming request bodies
- Morgan - HTTP request logger middleware
- Method-override - Middleware for overriding HTTP methods
- Debug - Tiny debugging utility
- Bluebird - Promise library
- Jade - Template engine for Express
- Http-errors - Create HTTP errors
- Cookie-parser - Parse cookie headers
The application is structured as follows:
MongoGridFS-FilesBucket/
βββ bin/
β βββ www
βββ public/
| βββ stylesheets/
| βββ style.css
βββ routes/
| βββ image.js
βββ views/
| βββ error.jade
| βββ index.jade
| βββ layout.jade
βββ controllers/
| βββ imageController.js
βββ helpers/
| βββ response-objects.js
βββ models/
| βββ image.js
| βββ pdf.js
| βββ video.js
βββ .env
βββ .gitignore
βββ app.js
βββ config.js
βββ package-lock.json
βββ package.json
βββ README.md
Contributions are welcome! Please feel free to submit a Pull Request.
- Vikash Kumar - vikashkrdeveloper
This project is licensed under the MIT License - see the LICENSE file for details.