Mongodb – Store file in Mongo’s GridFS with ExpressJS after upload


I have started building a REST api using expressJS. I am new to node so please bear with me. I want to be able to let users upload a file directly to Mongo's GridFS using a post to the /upload route.

From what I understand in expressJS documentation the req.files.image object is available in the route after uploading, which also includes a path and filename attribute. But how can I exactly read the image data and store it into GridFS?

I have looked into gridfs-stream but I can't tie ends together. Do I first need to read the file and then use that data for the writestream pipe? Or can I just use the file object from express and use those attributes to construct a writestream? Any pointers would be appreciated!

Best Solution

Here's a simple demo:

var express = require('express');
var fs      = require('fs');
var mongo   = require('mongodb');
var Grid    = require('gridfs-stream');
var db      = new mongo.Db('test', new mongo.Server("", 27017), { safe : false }); (err) {
  if (err) {
    throw err;
  var gfs = Grid(db, mongo);
  var app = express();

  app.use(express.bodyParser());'/upload', function(req, res) {
    var tempfile    = req.files.filename.path;
    var origname    =;
    var writestream = gfs.createWriteStream({ filename: origname });
    // open a stream to the temporary file created by Express...
      .on('end', function() {
      .on('error', function() {
      // and pipe it to gfs

  app.get('/download', function(req, res) {
    // TODO: set proper mime type + filename, handle errors, etc...
      // create a read stream from gfs...
      .createReadStream({ filename: req.param('filename') })
      // and pipe it to Express' response


I use httpie to upload a file:

http --form post localhost:3012/upload filename@~/Desktop/test.png

You can check your database if the file is uploaded:

$ mongofiles list -d test
connected to:
test.png    5520

You can also download it again:

http --download get localhost:3012/download?filename=test.png
Related Question