title: Retrieving images stored in mongodb
body: <blockquote>
    <p>I have tried to retrieve images I uploaded and saved to mongodb but I am not
    able to retrieve back the image directly from db. There seems to be a lot of information about
    uploading files but not a lot of information about retrieving the
    uploaded images from the db. </p>
    
    <p>I would really appreciate your help. Thanks in advance.</p>
  </blockquote>
  
  <p>This is part of what is saved in mongodb:</p>
  
  <pre><code>{
      "_id": {
          "$oid": "58f2e56fd8ca891e02fc85db"
      },
      "path": "uploads/1492313455338_codi.jpg",
      "filename": "1492313455338_codi.jpg",
      "__v": 0,
      "article": {
          "$oid": "58f2e56fd8ca891e02fc85dc"
      }
  }
  </code></pre>
  
  <p>Schema:</p>
  
  <pre><code>var mongoose = require('mongoose');
  
  var ImageSchema = new mongoose.Schema({
    img: { data: Buffer, contentType: String }
  });
  
  module.exports = mongoose.model('Image', ImageSchema);
  </code></pre>
  
  <p>router.js</p>
  
  <pre><code>router.post('/blog/article/addPost', upload.single('image'), function(req, res, err) {
    var image = new Image ({
      path: req.file.path,
      filename: req.file.filename
    });
      image.save(function(err, image) {
            //other code
          });
  });
  </code></pre>
  
  <p>Part of ejs to upload file</p>
  
  <pre><code>&lt;form action="/blog/article/addPost" method="post" enctype="multipart/form-data"&gt;
          &lt;div class="form-group"&gt;
            &lt;label="name"&gt;Add a picture for your article&lt;/label&gt;
            &lt;input id="upload-input" type="file" class="form-control" name="image"/&gt;
          &lt;/div&gt;
  &lt;/form&gt;
  </code></pre>
  
  <p>ejs to retrieve file:</p>
  
  <pre><code>&lt;div class="col-md-4"&gt;
          &lt;form method="GET" action="/blog/article/&lt;%= image.id %&gt;"&gt;
            &lt;img src="&lt;%= image.path %&gt;" style="width:304px;height:228px;"&gt;
          &lt;/form&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>multer configuration:</p>
  
  <pre><code>var storage = multer.diskStorage({
    destination: function (req, file, cb) {
      cb(null, './public/uploads/')
    },
    filename: function (req, file, cb) {
      if (!file.originalname.match(/\.(png|jpeg|jpg|wav|tif|gif)$/)) {
        var err = new Error();
        err.code = 'filetype';
        return cb(err);
      } else {
        cb(null, Date.now() + "_" + file.originalname);
      }
  
    }
  });
  </code></pre>