multer 的唯一目的是向请求对象添加正文对象和文件对象吗?编辑和保存应该由不同的包来完成吗?

Is multer's sole purpose to add a body object and files object to the request object? Should editing and saving be done by different package?

我想弄清楚文件上传和处理在 Node.js 中是如何工作的,我不确定 multer 在这件事上的责任是什么。 multer 的唯一目的是向请求对象添加正文对象和文件对象吗?是否应该通过不同的包来编辑和保存文件到文件系统?

虽然我可以看到我可以设置 multer 自动将文件保存在 files 对象中,但似乎选项非常有限并且图像处理超出了 multer 的范围。这是否意味着我需要另一个专门处理图像处理的包?

该包是否会从文件对象中获取文件流缓冲区并将其制作成实际文件,然后将所有更改应用到它?

multer is just a middleware to handle data from request with multipart/form-data header, and you can't do image processing with that, although there are few packages that integrate image processing library with multer like multer-sharp or multer-sharp-s3(用于上传到 S3 存储桶)。

Is multer's sole purpose to add a body object and files object to the request object?

那个,还有你想存储文件的地方(multer storage)

Does that mean I need another package that specifically handles image processing?

是的,multer 不能做我上面提到的任何图像处理。您可以使用 sharp 之类的东西来进行图像处理。

Would that package take the file stream buffer from the files object and make it into an actual file, then apply all the changes to it?

是的,例如使用 sharp 库:

router.post('/upload',upload.single('image') ,async (req, res) => {
   const { filename: image } = req.file 

   await sharp(req.file.path)
    .resize(500)
    .jpeg({quality: 50})
    .toFile(
        path.resolve(req.file.destination,'resized',image)
    )
    fs.unlinkSync(req.file.path)

    return res.send('SUCCESS!')
})

例子取自this dev.to article