title: How to upload a file using node js streams and HTML forms?
body: <p>I want to upload a file using NodeJS streams and HTML forms. I have a simple server.
  It is working when I upload the file using Postman. But when I upload through HTML form, the file is uploaded but is not readable. How to do it?</p>
  <p>This is <code>index.js</code>:</p>
  <pre><code>const app = require('express')();
  const fs = require('fs');
  
  app.get('/', (req, res) =&gt; {
      res.sendFile(__dirname + '/index.html');
  });
  
  app.post('/file-upload', (req, res) =&gt; {
      const filePath = 'uploads/uploaded-file';
      const stream = fs.createWriteStream(filePath);
      req.pipe(stream);
      stream.on('close', () =&gt; {
          res.send({ status: 'success', filePath })
      });
  });
  
  // Start server
  app.listen(3000, () =&gt; console.log(&quot;The server is running at localhost:3000&quot;));
  </code></pre>
  <p>This is <code>index.html</code>:</p>
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;html&gt;
  &lt;head&gt;
      &lt;meta charset=&quot;utf-8&quot;&gt;
      &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1&quot;&gt;
      &lt;title&gt;&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
      &lt;h1&gt;File Upload&lt;/h1&gt;
      &lt;form method=&quot;post&quot; action=&quot;/file-upload&quot; enctype=&quot;multipart/form-data&quot;&gt;
          &lt;input type=&quot;file&quot; name=&quot;file&quot;&gt;
          &lt;input type=&quot;submit&quot; name=&quot;upload&quot;&gt;
      &lt;/form&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  <p>This is the postman request screenshot:
  <a href="https://i.stack.imgur.com/cjCgl.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/cjCgl.png" alt="postman screenshot" /></a></p>