title: Uploading images and inserting the image url to mysql with node
body: <p>I have a node.js application which uploads images and stores them in a folder called <code>uploads</code>.</p>
  
  <p>I want to store the image url inside of mysql database and then display all of the images which where uploaded with html.</p>
  
  <p>Im using <code>formidable</code> to upload the images.</p>
  
  <pre><code>app.get('/upload', function (req, res){
      res.sendFile(__dirname + '/index.html');
  });
  
  app.post('/up', function (req, res){
      var form = new formidable.IncomingForm();
  
      form.parse(req);
  
      form.on('fileBegin', function (name, file){
          file.path = __dirname + '/uploads/' + file.name;
      });
  
  
  
   form.on('file', function (name, file) {
          console.log('Uploaded ' + file.name);
  
          connection.query('SELECT * FROM images', (err, results) =&gt; {
              // Throw error if find function fails
              if (err) return (new Error(err));
  
              // Throw error if there is already a file with that name
              if (results[0]) return (new Error('File with that name already exists, please choose another name'));
  
              connection.query('INSERT INTO images', (error, results) =&gt; {
  
  
                  // Throw error if save function fails
                  if (err) return (new Error(err));
  
                  // Throw error if you cannot verifty the save occured.
  
                  if (results.affectedRows !== 1) return (new Error('There was an unexpected error. Please try again'));
                  // Send Log to signal successfull upload
                  console.log('Uploaded ' + file.name);
              });
          });
      });
  });
  </code></pre>