I am unable to get return value from nodejs function to post request. I need "response" from generateHash and want to store in "hashVal" in the above post request
app.post('/filehash', function(req, res) {
var filePath = req.body.filePath;
if (!filePath) {
res.json(getErrorMessage('\'filePath\''));
return;
}
var hashVal = fileHash.generateHash(filePath);
logger.info('hashVal---->>>> '+hashVal);
res.send(hashVal);
});
var generateHash = function(filePath) {
var algo = 'md5';
var shasum = crypto.createHash(algo);
var response = '';
var s = fs.ReadStream(filePath);
s.on('data', function(d) { shasum.update(d); });
s.on('end', function() {
var d = shasum.digest('hex');
logger.debug('\n====== Creating filehash \'' + d + '\' ======\n');
console.log(d);
response=d;
return response;
});
};