Express中间件无序执行

时间:2018-09-05 14:50:44

标签: node.js express middleware multer

我正在尝试将User ID传递给名为multer的{​​{1}}函数,以便我可以上传以upload为名称的图像。

User ID是一个User ID令牌,在使用前必须为JWT

我希望首先执行decoded函数,以便我可以将authenticate传递给User ID函数。但是实际上发生的是先执行upload,然后执行upload,这意味着我无法在上传文件之前检查用户是否已通过身份验证。

authenticate-

Upload

const storage = multer.diskStorage({ destination: (req, file, cb) => { cb(null, path) }, filename: (req, file, cb) => { // user ID fetched from the JWT cookie console.log(req.user) // this should be the plaintext User ID if the authenticate function ran first. req.myfiles = req.files // pass the file object to the next middleware cb(null, `image.jpg`) } }) const fileFilter = (req, file, cb) => { const mimeType = file.mimetype.toLowerCase() console.log(file) if (mimeType === 'image/jpeg' || mimeType === 'image/png') { // the uploaded file MUST be an image type console.log('filter success') cb(null, true) }else { console.log('file filter, failed') cb(null, false) // reject if the uploaded file is not an image } } -

authenticate

0 个答案:

没有答案