Q. How do I store the uploaded images in a local directory?

 A:
Use Multer’s diskStorage option:

const storage = multer.diskStorage({

  destination: (req, file, cb) => {

    cb(null, ‘uploads/’); // local folder

  },

  filename: (req, file, cb) => {

    const unique = Date.now() + ‘-‘ + file.originalname;

    cb(null, unique);

  }

});

const upload = multer({ storage });

 

Then apply the upload middleware to your route. After a successful upload, you can (in the route handler) respond with file paths or metadata to the client.

Back To Top