Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

209
Views
Multer middleware does not goes trough and leave request in pending state

I have a problem with multer middleware. I created the middleware and set the static express files to the images folder

app.use('/images', express.static(path.join(__dirname, 'images')));

The multer middleware

const multer = require('multer');

const MIME_TYPES = {
  'image/jpg': 'jpg',
  'image/jpeg': 'jpg',
  'image/png': 'png'
};

const storage = multer.diskStorage({
  destination: (req, file, callback) => {
    callback(null, 'images');
  },
  filename: (req, file, callback) => {
    const name = file.originalname.split(' ').join('_');
    const extension = MIME_TYPES[file.mimetype];
    callback(null, name + Date.now() + '.' + extension);
  }
});

module.exports = multer({storage: storage}).single('image');

After this, I attached the middleware to the router:

router.post('/add-avatar',      multer, addAvatar);

The next step is the problem. The middleware does not executing the function at all. I am trying everything...

exports.add = async (req, res, next) => {
    try {
        let url = req.protocol + '://' + req.get('host');
        let imageLink = req.file ? url + '/images/' + req.file.filename : '';

        res.send(imageLink);    
    } 
    catch (error) {
        console.log(error);
    }
}

I tried to delete the multer middleware and the requests goes thought. I also tried to upload file with postman. But the request is left in pending.. enter image description here

I dont know what im doing wrong...

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Try the code below instead:

const multer = require("multer");

const upload = multer({
  dest: "images/",
  limits: { fieldSize: 25 * 1024 * 1024 },
});

router.post("/add-avatar", upload.single("image"), async (req, res) => {
  let url = req.protocol + '://' + req.get('host');
  let imageLink = req.file ? url + '/images/' + req.file.filename : '';

  res.send(imageLink); 
});

module.exports = router;

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!