【问题标题】:can i acess res object in multer custom storage engine _handleFile function我可以在 multer 自定义存储引擎 _handleFile 函数中访问 res 对象吗
【发布时间】:2020-01-06 09:54:03
【问题描述】:

我需要访问multer自定义存储引擎_handleFile函数中的res对象有什么办法吗?目前只有reqfilecb

MyCustomStorage.prototype._handleFile = function _handleFile (req, file, cb) {
  this.getDestination(req, file, function (err, path) {
    if (err) return cb(err)

    var outStream = fs.createWriteStream(path)

    file.stream.pipe(outStream)
    outStream.on('error', cb)
    outStream.on('finish', function () {
      cb(null, {
        path: path,
        size: outStream.bytesWritten
      })
    })
  })
}

没有可访问的 res 对象

【问题讨论】:

  • 添加另一个参数并从调用 _handleFile 的位置传递它。我看到该函数根据请求将文件复制到另一个文件。为什么需要响应?

标签: node.js express multer


【解决方案1】:

res 对象在req.res 下可用。你可以自己测试一下:

app.use((req, res, next) => {
  console.log(req.res === res); // true
  next();
})

我没有找到任何文档,但你可以看到源代码here

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-07
    • 1970-01-01
    • 1970-01-01
    • 2010-12-29
    相关资源
    最近更新 更多