Once I have parameters from the client, I want to match that string from the file system and send matching data back to client. I am trying to get familiar with callbacks, so in the code below, when I call the callback it sends me the response with correct data but its throwing an error - see below, after the pasted code.
app.js:
        app.get('/serverSearch', function (req, res) {
            var searchTxt = req.query.searchTxt;
            dirDirectory.readDirectory(function(logFiles){
                // res.json(logFiles);
                if(logFiles){
                    searchFileService.readFile(searchTxt,logFiles,function(lines,err){
                                console.log('Logs',lines);
                                  if (err)
                                   return res.send();
                                   res.json(lines);
                            })
                }
            });
            console.log('Search text', searchTxt);
        });
service.js:
function readFile(str,logFiles,callback){
    searchStr = str;
    for(var i=0; i<logFiles.length; i++){
        if(logFiles[i].filename !== '.gitignore'){
            fs.readFile('logs/dit/' + logFiles[i].filename, 'utf8', function (err,data) {
              if (err) {
                return console.log(err);
              }
              inspectFile(data,callback);
              callback(result);
              result = [];
            });
        }
    }
}
function inspectFile(data,callback) {
    var lines = data.split('\n');              // get the lines
    lines.forEach(function(line) {             // for each line in lines
        if(line.indexOf(searchStr) != -1) {    // if the line contain the searchSt
            result.push(line);
            // then log it
            return line;
        }
    });
    cb(callback);
}
function cb (callback) {
    callback(result);
}
Error:
_http_outgoing.js:344
throw new Error('Can\'t set headers after they are sent.');
^
Error: Can't set headers after they are sent.
 
     
    