Node.js - 错误:Route.post()需要一个回调函数但得到一个[object Undefined]

时间:2017-11-22 07:43:47

标签: javascript node.js

index.js

var multer = require('multer');
var _storage = multer.diskStorage({
  destination: function (req, file, cb) {
   cb(null, 'uploads/')
  },
  filename: function (req, file, cb) {
   cb(null, file.originalname);
  }
})
var upload = multer({ storage: _storage })


var myupload = (upload.single('userfile'), function(req, res){ 
  res.cookie('filename', req.file.originalname, {signed:true});
  res.cookie('filesize', req.file.size, {signed:true});
  var filename = 'C:/nodejs/module/'+ req.file.path;

  hash_module.hash(filename, function(hashResult) {
     res.cookie('hash', hashResult, {signed:true}); 
     res.redirect('/hash')
  })
})

module.exports.myupload = myupload;

route_info

route_info: [
    {file:'./index', path:'/myupload', method:'myupload', type:'post'} 
 ]

我想执行'myupload'。但我有这个错误。

我不知道该怎么做。我怎么能改变它?

1 个答案:

答案 0 :(得分:0)

应该是这样的:

var myupload = function() {
(upload.single('userfile'), function(req, res){ 
  res.cookie('filename', req.file.originalname, {signed:true});
  res.cookie('filesize', req.file.size, {signed:true});
  var filename = 'C:/nodejs/module/'+ req.file.path;

  hash_module.hash(filename, function(hashResult) {
     res.cookie('hash', hashResult, {signed:true}); 
     res.redirect('/hash')
  })
})
}