2014/09/11

Node.js 搜尋目錄裡的所有的.html檔案,是否有某個字串



筆記,原文網址: http://stackoverflow.com/questions/6959462/in-node-js-reading-a-directory-of-html-files-and-searching-for-element-attribu

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
var fs = require('fs');

fs.readdir('/path/to/html/files', function(err, files) {
    files.filter(function(file) { return file.substr(-5) === '.html'; })
         .forEach(function(file) { fs.readFile(file, 'utf-8', function(err, contents) { inspectFile(contents); }); });
});

function inspectFile(contents) {
    if (contents.indexOf('data-template="home"') != -1) {
        // do something
    }
}