title: Storing data stream from POST request in GridFS, express, mongoDB, node.js
body: <p>I am trying to figure out how I can post an image directly to GridFS without storing it anywhere on the server as a temporary file first. </p>
  
  <p>I am using Postman (chrome ext.) to post a file, and I manage to store this post as a file using: </p>
  
  <pre><code>req.pipe(fs.createWriteStream('./test.png'));
  </code></pre>
  
  <p>I am also able to store directly to GridFS from a readStream when the readStream is created from a file on the server. (see code)</p>
  
  <p>I have the following files, <code>saveFromReq.js</code> which listens for the POST and basically just passes this on to the <code>savePic.js</code>. </p>
  
  <p>saveFromReq.js:</p>
  
  <pre><code>var express = require('express');
  var app = express();
  var savePic = require('./savePic');
  var fs = require('fs');
  var GridStore = require('mongodb').GridStore;
  var pic = './square.png';
  var picID;
  
  
  
  //When the following
  
   //var pic = fs.createReadStream('./square.png', {autoClose: true});
  
  //is not commented out, and 'req' is replaced with 'pic' in the savePic function,
  //the file square.png is stored correctly to GridFS
  
  app.post('/picture', function(req, res){
  
      savePic(req, function(id){});
      res.writeHead(200, {'Content-Type': 'text' });
      res.end("Sucsess!\n");
  
  });
  
  app.listen(process.env.PORT || 3413);
  </code></pre>
  
  <p>savePic.js:</p>
  
  <pre><code>var savePic = function(req, callback){
  
  
  var Db = require('mongodb').Db,
      MongoClient = require('mongodb').MongoClient,
      Server = require('mongodb').Server,
      ReplSetServers = require('mongodb').ReplSetServers,
      ObjectID = require('mongodb').ObjectID,
      Binary = require('mongodb').Binary,
      GridStore = require('mongodb').GridStore,
      Grid = require('mongodb').Grid,
      Code = require('mongodb').Code,
      BSON = require('mongodb').pure().BSON,
      assert = require('assert');
      fs = require('fs');
  
      //When the following 
  
        //req.pipe(fs.createWriteStream('./test.png'));
  
      //is not commented out, the correct image is stored to test.png, and 
      //the sequence after req.on("data"... starts
      //(That sequence does not start at all when this is commented out..)
  
  var fileId = new ObjectID();
  var db = new Db('testDB', new Server('localhost', 27017));
  // Establish connection to db
  db.open(function(err, db) {
  
  
    var gridStore = new GridStore(db, 'test', 'w');
  
    //open 
    gridStore.open(function(err, gridStore) {
      console.log("opened");
  
  
      req.on("data", function (data) {
          console.log("data recieved");
              gridStore.write(data, function (err, gridStore) {
                  if (err) {
                      console.log("error writing file");
                  }
              });
          });
       req.on("end", function () {
              gridStore.close(function (err, gridStore) {
                  if (!err) {
                      console.log("The file has been stored to database.");
                      db.close();
                  }
              });
          });
             req.pipe(gridStore);
  
  
        });
  
  
  
  
  });
  callback(fileId);
  };
  module.exports = savePic;
  </code></pre>
  
  <p>Any help would be greatly appreciated!</p>