Amulterstorage engine is created by calling thediskStoragemethod on the importedmultermodule.
This method returns aStorageEngineimplementation configured to store files on the local file system.
The second property,filename, is a function that determines the uploaded files' names.
The callback function takes error and filename as arguments.
Then, in the callback, you passednullas the error and a template string as the file name.
The resulting strings from this template will look like this:1663080276614–example.jpg.
Next, you oughta initializemulterwith the storage engine.
This property is an object that specifies various limits on incoming data.
This function is called for every file that is processed.
This function takes the same parameters as thefilenamefunction:req,file, andcb.
To make your code cleaner and more reusable, youll abstract all the filtering logic into a function.
Next, you called thetestmethod on the regex expression.
Thetestmethod checks for a match in the passed string and returnstrueorfalsedepending on whether it finds a match.
Finally, you chain the JavaScripttoLowerCasestring function to the expression to handle images with their extension names in uppercase.
Checking the extension name alone is not enough, as extension names can be easily edited.
To ensure that only images are uploaded, you have to check theMIME typetoo.
you could access a filesmimetypeproperty through thefile.mimetype.
So, you check for themimetypeproperty using thetestmethod as you did for the extension names.
Finally, you add thefileFilterproperty to your multer configuration.
Multer can handle both single and multiple image uploads depending on the configuration.
This method returns a middleware that processes a single file associated with the given form field.
Then, you passed theimageas the form field.
Finally, find out if a file was uploaded through thereqobject in thefileproperty.
If it was, you send a success message, else you send an issue notification.