title: multer/node file upload works perfectly for files <20kb, hangs on files 20kb-1mb, and returns 502 error for files >1mb
body: <p>I'm working on a simple file upload site and have been having trouble consistently uploading files without error. The code I have now works perfectly when a file to be uploaded is less than ~20kb, but for files larger than that but smaller than ~1mb, only about ~20kb of the file is uploaded (thus corrupting the file).</p>
  
  <p>For files larger than 1mb, the upload fails completely and returns a 502 error. I've been sure to increase the multer filesize/fieldsize limit and never receive a file too large error. </p>
  
  <p>when a file between ~20kb-1mb is uploaded, there is no frontend or backend error, but multer seems to 'hang' on the upload and never reaches the upload callback function.</p>
  
  <p>EDIT I've removed the frontend code since this appears to be a node/config error</p>
  
  <p><strong>app.js file</strong> (irrelevant code removed)</p>
  
  <pre><code>const express    = require('express');
  const multer     = require('multer');
  const path       = require('path');
  const bodyParser = require('body-parser');
  const glob       = require('glob');
  const fs         = require('fs');
  
  const app  = express();
  const PORT = process.env.PORT || 3000;
  
  const storage = multer.diskStorage({
      destination: function(req, file, callback) { callback(null, './public/file') },
      filename: function(req, file, callback) { callback(null, req.fileCode + '-' + file.originalname) },
  });
  
  const upload = multer({storage: storage, limits: {fileSize: 1024 * 1024 * 1024, fieldSize: 10000000000}}).single('selectedFile');
  
  
  
  app.use(bodyParser.json({ limit: '5mb', extended: true })); //added from user suggestions
  app.use(bodyParser.urlencoded({ limit: '5mb', extended: true }));
  
  app.use(express.static(path.join(__dirname, 'public', 'media', 'styles.css')));
  app.use(express.static(path.join(__dirname, 'public', 'media', 'scripts.js')));
  
  app.get(["/","/index.html.var"], (req, res) =&gt; {
      res.sendFile(path.join(__dirname, 'public', 'index.html'));
  });
  
  app.post('/upload-file', function(req, res) {
      let fileCode = genFileCode();
  
      glob("**/public/file/" + fileCode + "*.*", function (err, files) {
          if(files[0])
              fileCode = genFileCode();
  
          req.fileCode = fileCode;
  
          console.log("check 1");
  
          upload(req, res, function(e) {
              console.log("check 2");
  
              if(e)
                  console.log("error " + e);
              else
                  console.log("no error");
          });
  
          res.send(fileCode);
      });  
  });
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/IVveJ.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/IVveJ.png" alt="enter image description here"></a>
  ^^exact same file (~370kb) is uploaded multiple times, all resulting in different sizes and none of which are the complete file (all are corrupted)</p>
  
  <p><a href="https://i.stack.imgur.com/b7w4y.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/b7w4y.png" alt="enter image description here"></a></p>
  
  <p>^^502 error results when any file larger than ~1mb is uploaded</p>