title: Meteor/Node writeFile crashes server
body: <p>I have the following code:</p>
  
  <pre><code>Meteor.methods({
    saveFile: function(blob, name, path, encoding) {
      var path = cleanPath(path), fs = __meteor_bootstrap__.require('fs'),
        name = cleanName(name || 'file'), encoding = encoding || 'binary',
        chroot = Meteor.chroot || 'public';
      // Clean up the path. Remove any initial and final '/' -we prefix them-,
      // any sort of attempt to go to the parent directory '..' and any empty directories in
      // between '/////' - which may happen after removing '..'
      path = chroot + (path ? '/' + path + '/' : '/');
  
      // TODO Add file existance checks, etc...
      fs.writeFile(path + name, blob, encoding, function(err) {
        if (err) {
          throw (new Meteor.Error(500, 'Failed to save file.', err));
        } else {
          console.log('The file ' + name + ' (' + encoding + ') was saved to ' + path);
        }
      }); 
  
      function cleanPath(str) {
        if (str) {
          return str.replace(/\.\./g,'').replace(/\/+/g,'').
            replace(/^\/+/,'').replace(/\/+$/,'');
        }
      }
      function cleanName(str) {
        return str.replace(/\.\./g,'').replace(/\//g,'');
      }
    }
  });
  </code></pre>
  
  <p>Which I took from this project
  <a href="https://gist.github.com/dariocravero/3922137" rel="nofollow noreferrer">https://gist.github.com/dariocravero/3922137</a></p>
  
  <p>The code works fine, and it saves the file, however it repeats the call several time and each time it causes meteor to reset using windows version 0.5.4. The F12 console ends up looking like this: <img src="https://i.stack.imgur.com/8zrLz.jpg" alt="enter image description here">. The meteor console loops over the startup code each time the 503 happens and repeats the console logs in the saveFile function. </p>
  
  <p>Furthermore in the target directory the image thumbnail keeps displaying and then display as broken, then a valid thumbnail again, as if the <code>fs</code> is writing it multiple times.</p>
  
  <p>Here is the code that calls the function:</p>
  
  <pre><code>"click .savePhoto":function(e, template){
      e.preventDefault();
       var MAX_WIDTH = 400;
      var MAX_HEIGHT = 300;
      var id = e.srcElement.id;
      var item = Session.get("employeeItem");
      var file = template.find('input[name='+id+']').files[0];
    // $(template).append("Loading...");
    var dataURL = '/.bgimages/'+file.name;
      Meteor.saveFile(file, file.name, "/.bgimages/", function(){
          if(id=="goodPhoto"){
              EmployeeCollection.update(item._id, { $set: { good_photo: dataURL }});
          }else{
              EmployeeCollection.update(item._id, { $set: { bad_photo: dataURL }});
          }
          // Update an image on the page with the data
          $(template.find('img.'+id)).delay(1000).attr('src', dataURL);
      });     
  
  
  
  },
  </code></pre>
  
  <p>What's causing the server to reset?</p>