问题描述
我使用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);
});
$ b s.on('end',function(){
var d = shasum.digest('hex');
console.log(d +''+ filename );
});
我想知道如何保存文件的哈希值以便后续匹配。任何想法都会受到欢迎。
简单的解决方案:将它写入一个文件(每个文件一个附加信息文件
更复杂一点的解决方案:保存一个包含所有哈希(如路径,哈希)的文件。您必须加载文件,解析数据并在每次之后保存。
正确的解决方案:使用数据库。使用SQLite应该很容易,而且不需要单独的数据库服务器。以下是NodeJS驱动程序的一个示例:
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.
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
这篇关于将散列与Node.js一起存储的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!