giokokos
giokokos

Reputation: 622

Store hash with Node.js

I am using Node.js to generate a hash for a file and then when it is changed, I want to match it with the previous version before saving it. For example, if no change has happened, no save will be done.

I try the following code to do the task (inspired by doc of Node.js):

var filename = __dirname + '/public/team.html';
var shasum = crypto.createHash('sha1');

var s = fs.ReadStream(filename);
s.on('data', function(d) {
  shasum.update(d);
});

s.on('end', function() {
  var d = shasum.digest('hex');
  console.log(d + '  ' + filename);
});

I was wondering how I can save the hash of the file in order to match it afterwards. Any ideas would be more than welcome.

Upvotes: 1

Views: 1301

Answers (1)

freakish
freakish

Reputation: 56467

Simple solution: just write it to a file (one additional info file per file you are saving).

A bit more complex solution: keep one file with all hashes like pairs (path, hash). You have to load the file, parse the data and save it afterwards each time.

Proper solution: use database. It should be very easy with SQLite and you won't need a separate database server. Here's an example of a driver for NodeJS:

https://github.com/developmentseed/node-sqlite3

Upvotes: 1

Related Questions