title: How to upload images and keep track of them using a database using multer or busboy?
body: <p>I am trying to upload images or pdfs (files in general) using express and the two modules i have come across are multer and busboy. The image uploading process is easy however i have come across problems with both of them. What i want is to upload the image with a name that I can later plug into a database too so that when i want to retrieve the file i can simply put the name of the file in the img tag and be done with it. In Busboy req.body for some reason is empty so I cannot pass anymore information using a form such as the username of the person. In multer we cannot customly name it we have to specify the filename inside the initial setup of multer. What i want is to have a form which enables us to enter a username and a file. Then i can using that username generate a random string which can be the same for both the file and the entry i will make into the database so that I can easily grab it later. Both the busboy and multer setup and usage is given below:</p>
  
  <p>busboy setup:</p>
  
  <pre><code>router.post('/profileUpload', function(req,res){
  let username = 'dual'
  let busboy = new Busboy({ headers: req.headers });
  busboy.on('file', function(fieldname, file, filename, encoding, mimetype) {
    let saveTo = './public/profileimage/'+`${username}.jpg`;
    console.log('Uploading: ' + saveTo);
    file.pipe(fs.createWriteStream(saveTo));
  });
  busboy.on('finish', function() {
    console.log('Upload complete');
  });
  return req.pipe(busboy);
  </code></pre>
  
  <p>})</p>
  
  <p>Multer setup:</p>
  
  <pre><code>const multer = require('multer');
  var storage = multer.diskStorage({
  destination: './public/reports',
  filename: function (req, file, cb){
  cb(null,'report' + Date.now() + path.extname(file.originalname))
  }
  })
  
  var upload = multer({ storage: storage })
  router.post('/reportUpload',upload.single('doc'),function(req,res){
  let usernameLab = req.session.usernameLab
  let username = req.body.patientUsername 
  var d = new Date()
  var uniqstring = Date.now()
  let uniqname = 'report' + uniqstring
  var date = `${d.getFullYear()}/${d.getMonth()+1}/${d.getDate()}` 
  let testName = req.body.testName
  let department = req.body.department
  let params = [username, usernameLab, uniqname, date, testName, department]
  let promise = queries.reportUpload(params)
  promise.then(function(){
      res.redirect('/reportUpload?upload=success')
      })
  });
  </code></pre>