【发布时间】:2023-03-09 18:28:01
【问题描述】:
我有一个需要 multipart/form-data 的表单,因为该表单中有一个文件字段。
我正在使用 ExpressJS,我使用 multer 来处理 multipart/formdata。
现在我还需要验证标头中的字段和 JWT。
在我的应用程序中,我使用中间件作为应用程序/JSON 类型,并这样做:
app.delete("/ticket/:ticketId", [
authValidation.validJWTNeeded,
authValidation.verifyIfNotLoggedOut,
authPermission.hasPermissionOrIsSameUser(staffRole),
ticketController.deleteById
]);
在每个中间件中,我传递参数(req、res、next)并在需要时返回 next,我工作正常。
如果不先发送文件,我就无法使用 multer 进行操作。 在文档中,他们会做这样的事情,这不是我想要的,因为文件已经发送:
app.post("/route",
multer.upload.single("avatar"),
function (req, res, next) {
// req.file is the `avatar` file
// req.body will hold the text fields, if there were any
})
)
我尝试过这样做,但没有成功:
app.post("/ticket", [
async (req, res, next) => {
multer.upload.none();
let jwt = //req.headers....... this is the jwt sent
//req.body should have all the fields
return next
},
authValidation.validJWTNeeded, //Here i verify the JWT for auth
authValidation.verifyIfNotLoggedOut, //About the same here
ticketController.verifyFields, // /!\ I need to verify if fields are correct
//function/middleware to upload using multer.upload.single("field"),
ticketController.insert // If everything above passed, then create the ticket and upload the file
]);
所以我的问题是:如何使用 multer 和通过 multipart/formdata 加密发送的数据进行所有验证?
谢谢!
【问题讨论】: