Listen for Folder or File Changes Using “Node.js”

Posted on
By Addam Driver

nodejs

Here is a quick tutorial on using fs.watchFile and fs.readFile in node.js. This will “listen” or “watch” for file changes on the server and then spit the contents of the file out to the console. However once you get a handle to the data you can do something more useful with it. Let’s check it out.


FIRST, let’s create a simple xml file (or any random file) inside of the same directory as the server.js file and call it “test.xml”. You can always move it but it’s location is really just for this example. Feel free do move it where you want.


<? xml version="1.0" encoding="UTF-8"?>
<names>
<name>"Addam Driver"</name>
</names>

SECOND, you want to setup a simple node.js, server.js file (this is assuming you have node.js installed already). We want to add the FS (filesystem) API. You also want to add a variable to hold the file name of the directory or file you want to listen for. In this example I just created a simple XML file but you can use any file type you want.


var fs = require('fs'); // require the filesystem api
var file2watch = "test.xml"; // which file to watch

THIRD, create a callback function that will do something with the messages coming back from the fs.watchFile api. The fs.watchFile returns to data pieces.

  1. curr – current file state
  2. prev – previous file state

Now what we will do in this function is compare the “current size” of the document against the “previous size” of the document. IF they don’t match, then we can assume the file has changed. At that point we will call fs.readFile, pass in the encoding of the file we expect “utf8″ and then call “readXML” which will do something useful with the file’s information.


function iSeeYou(curr, prev){
if(curr.size != prev.size){ // if there's been a change to the file (size change)
fs.readFile(file2watch,'utf8', readXML) // read the file contents, call readXML
}
}


FOURTH, create a callback function that will do something with the messages coming back from the fs.readFile api. The fs.readFile returns to data pieces.

  1. err – error message
  2. data – contents of the file


function readXML(err,data){ // fucntion for reading XML files
if (err){ // if there's an error
throw err; // throw the error message
}
console.log(data); // show the data in the console.
}


FIFTH, now that we’ve got the function’s n’ stuff in place, let’s add fs.watchFile to invoke the watching of the file.


fs.watchFile(file2watch, iSeeYou); // add a watcher to a file


Here is everything you should have in your server file to make this work:


var fs = require('fs'); // require the filesystem api
var file2watch = "test.xml"; // which file to watch

function readXML(err,data){ // function for reading XML files
if (err){ // if there's an error
throw err; // throw the error message
}
console.log(data); // show the data in the conosle.
}

function iSeeYou(curr, prev){
if(curr.size != prev.size){ // if there's been a change to the file (size change)
fs.readFile(file2watch,'utf8', readXML) // read the file contents, call readXML
}
}
fs.watchFile(file2watch, iSeeYou); // add a watcher to a file



CONGRATS!! You’ve now setup a file on your node server to be watched! So run this puppy and start changing the XML file. Every time you make a change and “save” the document, it should render the new contents in the server’s console. Instead of rendering it in a console, you can always process the information in the file and put it in a DB or do some other kind of server side stuff to it.

I hope this helps someone out there!
-A-

Tags: , , , , , , , , ,

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

CSS Methods

HTML Goodies

JavaScript

mustache

node.js

The HTML Writers Guild IWA