title: NodeJs how to ungzip data received with post on http
body: <p>Hello I have to do an homework and I don't know why not working. I have to handle a POST request, in witch a gz file is sent and I have to decompress it in the path specified by path parameter... I don't know why it doesn't work... maybe I am stupid at using curl command :P</p>
  
  <pre><code>function handle_post(req,res,path,cb){
  
      function warn(err,event){
          if(err)
              console.warn(`Request ${event}, could not close ${path}`)
          else
              console.warn(`Request ${event}, ${path} closed succesfully`)
      }
  
      if( Fs.existsSync(path) )
          return cb(400,JSON.stringify({error: "file already exist"}));
  
      var out= Fs.createWriteStream(path);
      res.pipe( Zlib.createGunzip() ).pipe( out );
  
      out.on('finish',() =&gt; cb(200,JSON.stringify({error : null, "written bytes":out.bytesWritten})+'\n'));
      req.on('aborted',() =&gt; warn(err,'aborted') );
      req.on('error',(err)=&gt;{
          cb(500,JSON.stringify({error : err.message, "written bytes":ws.bytesWritten})+'\n')
      });
  }
  
  var s=Http.createServer(
      (req,res) =&gt; {
          console.log("Request: "+req.method+" URL: "+req.url);
  
          function send(code,json_string){
              res.writeHead(code,{"Content-Type" : "application/json"});
              res.end(json_string)
          }
  
          if(req.method=='GET')
              handle_get(home+req.url,send);
          else if(req.method=='POST'){
              var parsed_url=Url.parse(req.url,true);
              var path=parsed_url.query.path;
              if(!path)
                  return send(400,JSON.stringify({error : 'Missing path'})+'\n');
              handle_post(req,res,home+'/'+path,send);
          }
      });
  s.listen(8080);
  </code></pre>