title: How do I upload multiple files to NodeJS using Golang?
body: <p>I'm trying to upload few files to a NodeJS server using Golang.
  I've managed to send one file at a time, one after another. Since this is a bad practice, I want to know how to send multiple files within one POST request.</p>
  <p>This is the client:</p>
  <pre><code>func uploadFiles() {
      for _, file := range filesToSend {
          requestBody := &amp;bytes.Buffer{}
  
          fileWriter := multipart.NewWriter(requestBody)
  
          part, _ := fileWriter.CreateFormFile(&quot;file&quot;, file)
  
          currentFile, _ := os.Open(file)
          defer currentFile.Close()
  
          io.Copy(part, currentFile)
  
          fileWriter.Close()
  
          r, _ := http.NewRequest(&quot;POST&quot;, &quot;http://localhost:6969/uploadData&quot;, requestBody)
          r.Header.Add(&quot;Content-Type&quot;, fileWriter.FormDataContentType())
          client := &amp;http.Client{}
  
          defer client.Do(r)
      }
  }
  </code></pre>
  <p>This is the NodeJS handling (using multer):</p>
  <pre><code>app.post('/uploadData', upload.single('file'), (req, res) =&gt; {
      const ip = req.socket.remoteAddress.replace(/:/g, '-');
      const file = req.file;
  
      fs.readFile(file.path, 'utf-8', (err, data) =&gt; {
          if (err) {
              res.send('error');
              return;
          }
  
          if (!fs.existsSync(__dirname + `\\data\\${ip}`))
              fs.mkdirSync(__dirname + `\\data\\${ip}`);
  
          fs.writeFile(__dirname + `\\data\\${ip}\\${file.originalname}`, data, (err) =&gt; {
              if (err) 
                  console.log(err);
  
              fs.unlinkSync(file.path);
  
              res.sendStatus(200);
          });
      });
  });
  </code></pre>