title: Get uploaded file as stream using Express
body: <p>Right now, I'm trying to make an API, and one of its functions is to take a file and upload it to another website, sort of like uploading a file through the API as some sort of "proxy".</p>
  
  <p>The website I'm sending the file to requires additional steps to work (namely sending the destination in another request) and I wanted to have my API do that all in one request instead, where that request already had all the details needed.</p>
  
  <p>I've gotten the request for the destination done, but I'm having a hard time finding out how to "pipe" the upload of a user into the request that I send to the website. <code>request-promise</code> takes a file stream as an input when uploading files, so I wanted to hopefully pipe the upload coming from the user to the request. That way I don't have to use any disk space to temporarily save the file, especially since I'm very limited in disk space and since the server I'm sending it to virtually has no file limit.</p>
  
  <p>As of now, I'm using <code>express-fileupload</code> to temporarily save the file, but this will eventually blow up my extremely small (194 MB) allocated space. I'm using <code>express</code> v4.16.4 and Node v12.0.0. For the requests, I use <code>request-promise</code> v4.2.4.</p>
  
  <p>Here's how I declared the Express app and the <code>express-fileupload</code> object.</p>
  
  <pre class="lang-js prettyprint-override"><code>var router = express.Router({ caseSensitive: true });
  app.use(router);
  
  ...
  
  router.use(require("express-fileupload")({
      useTempFiles : true,
      tempFileDir : ".data/temp"
  }));
  </code></pre>
  
  <p>And here's what I'm using to toss the file into the website:</p>
  
  <pre class="lang-js prettyprint-override"><code>if (req.body.id === undefined || req.files.file === undefined)
      return new APIError(400);
  if (!Object.keys(JSON.parse(fs.read(".data/master_list_of_possible_values_for_that_variable.json"))).includes(req.body.id))
      return new APIError("400-ID");
  
  var session_flag = await(session.executeRequest({
      method: "POST",
      uri: Constants.URL.upload.session,
      form: {
          id: req.body.id
      }
  })
      .then(r =&gt; {return r;})
      .catch(e =&gt; { return e; }));
  
  if (session_flag instanceof Error)
      throw session_flag;
  
  var upload = await(session.executeRequest({
      method: "POST",
      uri: Constants.URL.upload.fileEndpoint,
      formData: {
          file: {
              value: fs.createReadStream(req.files.file.tempFilePath),
              options: {
                  filename: req.files.file.name,
                  contentType: req.files.file.mimetype
              }
          }
      }
  })
      .then(r =&gt; {return r;})
      .catch(e =&gt; { return e; }));
  
  if (upload instanceof Error)
      throw upload;
  
  return { success: true };
  </code></pre>