Store file in Mongo's GridFS with ExpressJS after upload

Maarten picture Maarten · May 10, 2013 · Viewed 9.7k times · Source

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!

Answer

robertklep picture robertklep · May 10, 2013

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("127.0.0.1", 27017), { safe : false });

db.open(function (err) {
  if (err) {
    throw err;
  }
  var gfs = Grid(db, mongo);
  var app = express();

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

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

  app.listen(3012);
});

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: 127.0.0.1
test.png    5520

You can also download it again:

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