title: Save file uploads to Mongo DB using Express
body: <p>Following <a href="http://www.hacksparrow.com/handle-file-uploads-in-express-node-js.html" rel="noreferrer">this tutorial</a>, I have managed to create a form with a <code>file</code> input that uploads files to a specified directory. That's dandy and all, but it doesn't save anything to a database and I don't have any reference to the files that were uploaded to display in a Jade template.</p>
  
  <p>Here's what I'm doing:</p>
  
  <pre><code>// add new bulletin
  exports.addbulletin = function(db) {
      return function(req, res) {
  
          var tmp_path = req.files.coverimage.path;
          // set where the file should actually exists - in this case it is in the "images" directory
          var target_path = './public/uploads/' + req.files.coverimage.name;
          // move the file from the temporary location to the intended location
          fs.rename(tmp_path, target_path, function(err) {
              if (err) throw err;
              // delete the temporary file, so that the explicitly set temporary upload dir does not get filled with unwanted files
              fs.unlink(tmp_path, function() {
                  if (err) throw err;
                  res.send('File uploaded to: ' + target_path + ' - ' + req.files.coverimage.size + ' bytes');
              });
          });
  
          // Submit to the DB
          collection.insert(req.body, function (err, data) {
              if (err) {
                  res.send("There was a problem adding the information to the database.");
              }
              else {
                  res.location("index");
                  res.redirect("/");
              }
          });
  
      }
  };
  </code></pre>
  
  <p>The second part of that is inserting the <code>req.body</code> (the other form fields that aren't file inputs) to a Mongo database. I would like to just tack on <code>req.files</code> along with where it's inserting the <code>req.body</code>, but that doesn't work. </p>
  
  <p>My confusion kind of lies with how Mongo works too, I think. I'm no database expert, but when you "upload" an image, should the actual image file go to the database, or should just a reference (like the image name and location of it in the app) get added?</p>
  
  <p>So in summary, I just want to save the uploaded image (or a reference to it) to the Mongo database so that I can then reference it in a Jade template to be displayed to the user.</p>
  
  <p>For what it's worth, here's the relevant parts of the form (using Jade for templating):</p>
  
  <pre><code>form.new-bulletin(name="addbulletin", method="post", enctype="multipart/form-data", action="/addbulletin")
    input(type="file", name="coverimage")
  </code></pre>
  
  <p><strong>UPDATE</strong></p>
  
  <p>I forgot to add that I'm using <a href="https://www.npmjs.org/package/connect-multiparty" rel="noreferrer">connect-multiparty</a></p>
  
  <pre><code>multipart = require("connect-multiparty"),
  multiparty = multipart();
  app.post("/addbulletin", multiparty, routes.addbulletin(db));
  </code></pre>