Search code examples
node.jsexpressmulter

can i acess res object in multer custom storage engine _handleFile function


i need to acess the res object in multer custom storage engine _handleFile function is there any way? currently there is only req, file, cb

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
      })
    })
  })
}

there is no res object to acess


Solution

  • The res object is available under req.res. You can test it yourself:

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

    I didn't find any documentation, but you can see the source here.