title: Sending large image data over HTTP in Node.js
body: <p>In my development environment I have two servers. One sends and image to the other over a <code>POST</code> http request.</p>
  
  <p>Client server does this:</p>
  
  <pre><code>    fs.readFile(rawFile.path,'binary',function (err, file){
          restler.post("http://0.0.0.0:5000",{
              data: file,
              headers:{
                  "Content-Type": rawFile.type,
              }
          }).on('complete',function(data,response){                               
              console.log(data);
              res.send("file went through")
          })
  </code></pre>
  
  <p>The server that recieves the request does this:</p>
  
  <pre><code>    server.post('/',function(req,res,next){
          fs.writeFileSync("test.png",req.body,"binary",function(err){
              if(err) throw err;
              res.send("OK")
          })
      })
  </code></pre>
  
  <p>If i send a small image it works fine. However, if i send a large image although the file is saved correctly only the first upper portion of the image is displayed. The rest is black. Image size is correct.</p>
  
  <p>I guess it's just the first chunk of the image that's being written on the file.
  I've tried creating a <code>readStream</code> and a <code>writeStream</code> but it doesn't seem to work:</p>
  
  <pre><code>req.body.pipe(fs.createWriteStream('test.png'))
  </code></pre>
  
  <p>Can i stream directly from the binary data and <code>pipe</code> it into the file? For what i've seen, <code>readStream</code> is often used to stream from files not raw binary data.</p>
  
  <p>I read a few <a href="https://stackoverflow.com/questions/14170210/nodejs-createwritestream-vs-writefile">post</a>s but it doesn't seem to work for me.</p>
  
  <p>I'm using <code>restler</code> module in the client server and <code>restify</code> in the other.</p>
  
  <p>Thanks!</p>