Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Node.js callback fires twice? Why?

node.js javascript

This topic has been archived. This means that you cannot reply to this topic.
No replies to this topic

#1 sacredheart

sacredheart

    CC Lurker

  • New Member
  • Pip
  • 8 posts

Posted 03 November 2013 - 12:22 PM

In the following code, why does the createFile callback fire twice? This only happens when the server below is processing two or more requests at the same time, not if only one request has been made. Output at the bottom of the post.

 

function myRequest(request, response) {
  function writeFile(filePath, request, callback) {
    newFilePath = "/home/pi/upload"+filePath; //filePath looks like this: /home/blah/file.txt, the code below creates this structure under another directory, so newFilePath becomes /home/pi/upload/home/blah/file.txt
    tempFileName = path.basename(filePath)+".temp";
    console.log("Processing "+filePath+"->"+newFilePath+" with tempname " +tempFileName);
    var createFile = request.pipe(fs.createWriteStream(tempFileName));
    createFile.on("finish", function(error) { //Why does it fire the callback twice?
      if(error) {
        throw error;
      } else {
        moveFile(tempFileName, newFilePath, function(error) {
          if(error) {
            throw error;
          } else {
            console.log("OK");
          }
        });
      }
    });
  }
 
  function moveFile(tempFileName, newFilePath, callback) {
    dirPath = path.dirname(newFilePath);
    fs.stat(dirPath, function(error, stats) {
      if(error == null) {
        console.log(dirPath+" already exists");
        fs.stat(tempFileName, function(error, stats) {
          if(error == null) {
            console.log("OK, writing "+newFilePath);
            fs.rename(tempFileName, newFilePath, function(error) {
              if(error) { //Error on the second run, because the file has been moved in the first run, shouldn't happen?
                throw error;
              } else {
                var myCB = JSON.stringify({fMove: "OK"});
                callback(myCB);
              }
            });
          } else {
            console.log("File exists");
          }
        });
      }
    });
  }
  writeFile(fileName, request, function() {
    //Do some stuff
  });
  request.on("end", function() {
    //Do other stuff
  }
});
 
http.createServer(myRequest).listen(8888);
 
 
Output from my script
 
Processing /home/pi/app/temp/client.js->/home/pi/upload/home/pi/app/temp/client.js with tempname client.js.temp
/home/pi/upload/home/pi/app/temp already exists
/home/pi/upload/home/pi/app/temp already exists
OK, Writing /home/pi/upload/home/pi/app/temp/client.js
OK, Writing /home/pi/upload/home/pi/app/temp/client.js
 
/home/pi/app/server.js:67
            throw error;
                  ^
{"fMove":"OK"}
 

 






Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download