title: save mp3 as ajax post data with node js
body: <p>Not sure what am I doing wrong...</p>
  
  <p>I have a node js webserver that should save a mp3 file to disk from an ajax post. The post contains an mp3 file constructed from a wav file created by recorder.js in the browser.</p>
  
  <p>In my requestHandler I have the following code:</p>
  
  <pre><code>var
  formidable = require('formidable'),
  http = require('http'),
  path = require('path'),
  fs = require('fs');
  
  function requestHandler(req, res) {
  
   var requestPath = path.basename(req.url) || 'index.html',
       ext = path.extname(requestPath),
       localFolder = __dirname + '/public/',
       uploadFolder = __dirname + '/uploads/';
  
  if(requestPath === 'uploadmp3' &amp;&amp; req.method === 'POST') {
  
      var form = new formidable.IncomingForm();
  
      form.parse(req, function(err, fields, files) {
          if(err) {
              console.error(err.message);
              return;
          }
          var mp3File = uploadFolder + fields.fname;
          var base64File = new Buffer(fields.data, 'binary').toString('base64');
          var decodedFile = new Buffer(base64File, 'base64').toString('binary');
  
          fs.writeFileSync(mp3File, decodedFile, 'base64', function(err){
              if(err) {
                  return console.log(err);
              }
              console.log('written to disk: ' + mp3File);
          });
  
          res.writeHead(200, {'Content-Type:' : 'text/plain'});
          res.write(mp3File+ ' \n\n');
          res.end('\n');
      });
  
      return;
    }
  }
  </code></pre>
  
  <p>The result is that I save the file to disk, but although it has the correct size, the time is not set properly and the playback last for one second.</p>
  
  <p>I can save it with php with no problem... mp3 file plays perfectly, but I really need this to work on node js.</p>