Answer the question
In order to leave comments, you need to log in
How to deal with template code, redundant decomposition?
Good evening, one of the main problems of my projects is repetition and template code, which violates DRY, I strive for maximum decomposition, but in my opinion this is often redundant and complicates the project.
For example, there is a middleware Multer (file upload):
// categoryRouter.js
const fileFilter = (req, file, cb) => {
const allowedMimeType = ['image/png', 'image/jpeg', 'image/jpg'];
if (allowedMimeType.includes(file.mimetype) {
cb(null, true);
} else {
cb(new Error('Invalid file type'));
}
};
const storage = multer.diskStorage({
destination: (req, file, cb) => {
cb(null, path.resolve('public/uploads/categories'));
},
filename: (req, file, cb) => {
cb(null, `${uuidv4()}${path.extname(file.originalname)}`);
}
});
router.post('/:slug/uploadImage', multer({ storage, fileFilter }).single('image'), (req, res) => {
// сохранить путь в бд
});
// fileHelper.js
exports.isImage = ({ mimetype }) => ['image/png', 'image/jpeg', 'image/jpg'].includes(mimetype);
exports.getUniqueFileName = (originalName) => `${uuidv4()}${path.extname(originalName)}`;
// fileFilter.js
module.exports = (fileValidation) => {
return (req, file, cb) => {
if (fileValidation(file)) {
cb(null, true);
} else {
cb(new Error('Invalid file type'));
}
};
};
module.exports = (destination) => {
const filter = fileFilter(fileHelper.isImage);
const storage = multer.diskStorage({
destination: (req, file, cb) => {
cb(null, destination);
},
filename: (req, file, cb) => {
cb(null, fileHelper.getUniqueFileName(file.originalname));
}
});
return multer({ storage, fileFilter });
};
const uploadImage = require('./multerFactory')('public/uploads/category');
router.post('/:slug/uploadImage', uploadImage.single('image'), (req, res) => {
// сохранить ссылку на файл в бд
});
Answer the question
In order to leave comments, you need to log in
1. To what extent is such an approach justified and necessary?Quite justified. Imagine that a month after the launch, you are faced with the task “the disk space is running out quickly, let’s store it in a different way, and don’t forget to change the download”, how will it be easier, fix one place or wherever you accumulated pastilles?
2. What is usually done in such cases?If some part of the code is repeated more than 1 time, then it is taken out into a separate function. So you have chosen the right direction.
3. Have I over-decomposed everything? (or even better, how?)Show a colleague if he can figure out what is happening without explanation?
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question