Node JSjs - 我想在应用程序中获取哈希.js



app.js

app.post('/upload', upload.single('userfile'), function(req, res){
  res.cookie('filename', req.file.originalname);
  res.cookie('filesize', req.file.size);
  var filename = __dirname +'/'+ req.file.path;
  console.log(hash(filename))
  res.cookie('hash', hash(filename));
  res.redirect('/hash')
})

哈希.js

var fs = require('fs');
var crypto = require('crypto');
var algorithm = 'sha256';
module.exports = function(filename){
  var shasum = crypto.createHash(algorithm);
  var s = fs.ReadStream(filename);
  s.on('data', function(data) {
    shasum.update(data)
  })
  var hash;
  s.on('end', function() {
    hash = shasum.digest('hex') // this hash, i want to get in app.js 
  })
}

我有两个代码。

在哈希.js中,我想在应用程序中获取"var hash.js

我该怎么做?我不知道该怎么办。

我需要你的帮助。

哈希.js

module.exports = function(filename){
  var module = {};
  var shasum = crypto.createHash(algorithm);
  var s = fs.ReadStream(filename);
  s.on('data', function(data) {
    shasum.update(data)
  })
  s.on('end', function() {
    module.hash = shasum.digest('hex') // this hash, i want to get in app.js 
  })
  return module
}

应用.js

var hash_module = require("./hash.js")(filename);
console.log(hash_module.hash)

最新更新