title: Answering request with multipart/form-data in Express.js
body: <p>I'm trying to communicate with an old PHP service, which uses json in multipart/form-data. The server send a POST request to my express.js server with some data and wait me to answer the same way.</p>
  
  <p>Parsing the requests is easy using multer. But I'm not able to send responses in same format, I'm trying to do this with form-data module (<a href="https://www.npmjs.com/package/form-data" rel="nofollow">https://www.npmjs.com/package/form-data</a>) but I could use any other solution. I need to create a multipart/form-data response and send it back to the server.</p>
  
  <p>Example code:</p>
  
  <pre><code>var express = require('express');
  var multer  = require('multer');
  var FormData = require('form-data');
  
  var app = express();
  
  
  app.post('/', multer().array(), function (req, res) {
    //Request is well handled and req.body contains fields from request
    //try to send response params in same multipart/form-data format.
  
    var form = new FormData();
    form.append('key', 'value');
    form.append('key2', 'value');
    res.set(form.getHeaders()); //Set content header
    res.set('content-length', ''+form.getLengthSync()); //Set length header
    form.pipe(res);  //Pipe form stream into response, response is send, but format is wrong.
  });
  </code></pre>
  
  <p>Any advice? I have seen lot of examples of multipart/form-data requests parsing but I need to send the responses to the server in same format.</p>