top of page

Handling File Uploads in MEAN Stack Using Multer and MongoDB GridFS

  • Writer: akanksha tcroma
    akanksha tcroma
  • Apr 16
  • 2 min read

Web apps allow users to upload files like images, documents, or videos to keep performance smooth. It is important to manage large files well. In the MEAN stack, Multer handles the upload process, and MongoDB GridFS stores the files safely. A MEAN Stack Certification helps you learn these skills and build better apps.


Mean Stack Developer Training
Mean Stack Developer Training

Setting Up File Upload Handling in MEAN Stack

Set up a Node.js server with Express.js to upload files using Multer and GridFS. Use the steps below to configure both.

Step 1: Install Required Packages

Start by installing the necessary packages:

 

npm install express multer mongoose gridfs-stream

 

●     express: Web framework for handling requests and routes.

●     multer: Middleware for handling file uploads.

●     mongoose: MongoDB ODM (Object Data Modeling) library.

●     gridfs-stream: Library for interacting with MongoDB GridFS.

 
Step 2: Configure Multer for File Uploads

We will store the files in memory before sending them to GridFS.

 

const multer = require('multer');

const storage = multer.memoryStorage();

const upload = multer({ storage: storage });

 

Step 3: Connect to MongoDB and Set Up GridFS

 

const mongoose = require('mongoose');

const gridfsStream = require('gridfs-stream');

mongoose.connect('mongodb://localhost:27017/mydatabase', { useNewUrlParser: true, useUnifiedTopology: true });

 

const conn = mongoose.connection;

conn.once('open', () => {

    const gfs = gridfsStream(conn.db, mongoose.mongo);

    gfs.collection('uploads');

});

 

Step 4: Create File Upload Route

 

app.post('/upload', upload.single('file'), (req, res) => {

    const gfs = gridfsStream(conn.db, mongoose.mongo);

    const writeStream = gfs.createWriteStream({

        filename: req.file.originalname,

        content_type: req.file.mimetype

    });

    writeStream.write(req.file.buffer);

    writeStream.end();

 

    writeStream.on('finish', (file) => {

        res.json({ file: file });

    });

});

 

Step 5: Retrieve Files from GridFS

Create a route to get files fetched from MongoDB GridFS.

 

app.get('/file/:filename', (req, res) => {

    const gfs = gridfsStream(conn.db, mongoose.mongo);

    gfs.files.findOne({ filename: req.params.filename }, (err, file) => {

        if (!file) {

            return res.status(404).send('File not found');

        }

        const readStream = gfs.createReadStream({ filename: file.filename });

        readStream.pipe(res);

    });

});

 

 

Why Learn MEAN Stack in Noida and Delhi?

If you want to explore the MEAN stack further, a MEAN Stack Course in Noida is an excellent choice. The course offers in-depth training on the entire stack and covers how to handle file uploads with Multer and MongoDB GridFS.


If you live in Delhi, a MEAN Stack Course in Delhi can also help. It gives full training on the MEAN stack and shows you how to manage file uploads using GridFS. You will learn to build strong and flexible web apps.

Conclusion

Multer and MongoDB GridFS are helpful tools for handling large file uploads in MEAN stack apps. Multer helps receive files, and GridFS stores them well. These tools work well in both Noida and Delhi courses to help you build better apps.

Comments


Get in touch and share your thoughts with us

© 2023 by itlearning. All rights reserved

bottom of page