Good Day all,
I am trying to upload a multi-part formdata using multer. The file uploads to the correct directory but then program execution stops. Im testing the route with postman. The middleware module is a self executing function that I pass to my route however after the file saves the console just displays "undefined" and postman throws Could not get response
After the file upload is complete it is supposed to move to the "updateRules" middleware to validate the form.
The funny thing is the "console.log(before return)" in my "fileUpload.js" that executes immediatly after I change & save the file.
I dont know if its the self invoking function that does this but im new to Node and Im just following a tutorial. The auth & updateRules middlewares work fine but program execution hangs after the file uploads to the specified path. Also the user.id is a digit like 6 for example
Below is my route
const router = require('express').Router()
const { validate } = require('../validators')
const { update } = require('../controllers/userController')
const { auth } = require('../middleware/auth')
const { rules : updateRules } = require('../validators/user/update')
const { userFile } = require('../middleware/fileUpload')
router.post('/update', [auth, userFile, updateRules, validate], update)
module.exports = router
Here is my fileUpload.js middleware
const multer = require('multer')
const fs = require('fs')
const path = require('path')
exports.userFile = ((req,res, next) => {
const getFileType = file => {
const mimeType = file.mimetype.split('/')
return mimeType[mimeType.length - 1]
}
const generateFilename = (req, file, cb) => {
console.log(file.originalName)
const extension = getFileType(file)
const filename = Date.now() + '-' + Math.round(Math.random() * 1E9 ) + '.' + extension
cb(null,file.fieldname + '-' + filename)
}
const fileFilter = (req, file, cb) => {
const extension = getFileType(file)
const allowedType = /jpeg|jpg|png/
const passed = allowedType.test(extension)
if(passed){
return cb(null, true)
}
return cb(null, false)
}
const storage = multer.diskStorage({
destination: function(req, file, cb) {
const { id } = req.user
const dest = `./uploads/user/${id}`
fs.access(dest, (error) => {
// if folder doesnt exist
if(error) {
console.log("folder doesnt exist")
return fs.mkdir(dest, {recursive: true} ,(error) => {
cb(error, dest)
})
} else {
console.log("folder exists")
fs.readdir(dest, (error, files) => {
if(error) throw error
for(const file of files) {
fs.unlink(path.join(dest, file), error => { if(error) throw error } )
}
})
return cb(null, dest)
}
})
},
filename: generateFilename
})
console.log("before return")
return multer({storage, fileFilter}).single('avatar')
})()
I also tried exporting storage & fileFilter functions, call multer from router but the rsult is the same
route.js
const multer = require('multer')
const { storage, fileFilter } = require('../middleware/fileUpload')
router.post('/update',
[auth, multer({storage, fileFilter}).single('avatar'),
updateRules, validate], update)