我無法從nodejs函數獲取返回值以發佈請求。我需要generateHash「響應」,並希望在「hashVal」存儲在上述職位要求無法在nodejs函數中返回值
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;
});
};