Storing images in the database

I’ve deployed a basic social app and i can fetch all the data submitted by users except images, because they do not get stored, hence I receive a Failed to load resource: the server responded with a status of 404 () cannot find

I installed multer gridfs and set it up but it still doesn’t work:

app.use(bodyParser.json({ limit: "30mb", extended: true }));
app.use(bodyParser.urlencoded({ limit: "30mb", extended: true }));
app.use('/assets', express.static('assets'));

const storage = multer.diskStorage({
  destination: function (req, file, cb) {
    cb(null, "public/assets");
  filename: function (req, file, cb) {
    cb(null, file.originalname);
const upload = multer({ storage });

/* ROUTES WITH FILES */"/auth/register", upload.single("picture"), register);"/posts", verifyToken, upload.single("picture"), createPost);

/* ROUTES */
app.use("/auth", authRoutes);
app.use("/users", userRoutes);
app.use("/posts", postRoutes);

Hi @Pitar_Petrov and welcome to the MongoDB community forum!!

Ideally, there are three basic ways to work with images in MongoDB. The forum post gives you a detailed description on how to use the recommended methods while working with images.

Since we do not have enough expertise on using multer with gridfs, I would recommend you see if this blog post Uploading Files to MongoDB with GridFS and Multer Using NodeJS is useful to you. Please note that this blog post is external to MongoDB, so we cannot guarantee the correctness of it but perhaps may be able to point you toward the right direction.
Further, you could visit the multer community for a more detailed response.

Finally, the official MongoDB documentation for GridFS with MongoDB would also be a recommended read.

Best Regards

1 Like