title: What do multer().single() returns?
body: <p>I came across how to handle error of multer , but I am unble to understand its flow .</p>
  <pre><code>var multer = require('multer')
  var upload = multer().single('avatar')
  app.post('/profile', function (req, res) {
  upload(req, res, function (err) {
  if (err) {
    // An error occurred when uploading
    return
  if (err instanceof multer.MulterError) {
    // A Multer error occurred when uploading.
  } else {
    // An unknown error occurred when uploading.
  }
  // Everything went fine
  // Everything went fine.
   })
   })
  </code></pre>
  <p>What I am unable to understand :-</p>
  <p>What do multer().single() returns and how are we using that(upload function) inside the router handler as shown in code ?</p>
  <p><strong>EDIT</strong> :</p>
  <p>I am unable to find the source code , what is wriiten inside multer for .single() function . Can anyone provide me link to it?</p>
  <p><strong>Edit 2</strong></p>
  <p>I have tried to handle error in the following way but it is not working . What's wrong with it ?</p>
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>const storage = new gridfsStorage({
      url : 'mongodb://localhost:27017/uploadeditems' ,
      file : (req , file) =&gt; {
          if(file.mimetype === 'image/jpeg') {
              return {
                  filename : file.originalname,
                  bucketName : 'Images'
              }
          }
          else if(file.mimetype === 'application/pdf') {
              return {
                  filename : file.originalname , 
                  bucketName : 'projectPDFs'
              }
          }
  
          else {
              return null
          }
      }
  })
  
  
  upload = multer({storage })
  
  
  app.get('/' , (req , res) =&gt; {
      res.render('upload')
  })
  
  app.post('/upload' , upload.single('pproject')  , async (req, res) =&gt; {
  
      res.render('upload' , {
              msg : "File has been uploaded successfully"
          })
  } ,
  
  (err , req , res) =&gt; {
    res.json({
      msg : "Some error occured"})
  )</code></pre>
  </div>
  </div>
  </p>
  <p>I am assuming if some error occurs , upload.single() will call next(err) , which will be caught by the last error handler .</p>