title: Node / Express / Request - Attempt to proxy POST request with parameters throws "Write after end"
body: <p>I'm attempting to proxy all /api/ requests from local node server to a remote server while embedding some authentication parameters to them.</p>
  
  <p>My current approach seems to work fine for GET with query parameters and POST as long as I don't give the "form" parameter to Request library, but as soon as I include it, the server starts throwing Error: write after end.</p>
  
  <pre><code>var express = require("express");
  var request = require("request");
  var parser = require("body-parser");
  var strftime = require("strftime");
  
  var app = express();
  var path = require("path");
  var port = 80;
  var apiUrl = "http://example.com/";
  var apiUser = "example";
  var apiPass = "example";
  
  app.use(express.static(path.join(__dirname, "/dist")));
  app.use(parser.json());
  app.use(parser.urlencoded({extended: true}));
  
  app.get("/api/*/", function(req, res) {
      console.log((strftime("%H:%M:%S") + " | GET -&gt; " + req.url));
      var url = apiUrl + req.url;
      req.pipe(request.get(url, {
          auth: {
              user: apiUser,
              pass: apiPass
          }
      })).pipe(res);
  });
  
  app.post("/api/*/", function(req, res) {
      console.log((strftime("%H:%M:%S") + " | POST -&gt; " + req.url));
      var url = apiUrl + req.url;
      req.pipe(request.post(url, {
          form: req.body, // &lt;----- RESULTS IN "write after end" error
          auth: {
              user: apiUser,
              pass: apiPass
          }
      })).pipe(res);
  });
  
  app.listen(port);
  console.log("Development server started, listening to localhost:" + port);
  console.log("Proxying /api/* -&gt; " + apiUrl + "/api/*");
  </code></pre>
  
  <p>This probably has to do with the body-parser middleware, but I couldn't find any fix for this issue and I don't quite get why the "form: req.body" would break the script. Logging the req.body seems to output the expected parameters.</p>
  
  <p>I also attempted the alternative chaining syntax with .form(req.body), but the result is the same.</p>