No 'Access-Control-Allow-Origin' header in some chrome version

87 Views Asked by At

I am trying to upload some images to cloudinary through my client, it works fine on localhost, and in chrome version 87, but in chrome v89 i get No 'Access-Control-Allow-Origin' header error, any ideas on why this is happening?

this is my server route:

const uploader = require('../config/cloudinary.config')

router.post('/upload', uploader.single("imageUrl"), (req, res) => {
    res.setHeader('Access-Control-Allow-Origin', 'client domain');
    if (!req.file) {
        res.status(500).json({ code: 500, message: 'Error loading the file' })
        return;
    }

    res.json({ secure_url: req.file.path })
})

and this is my cors config:

const cors = require('cors')

const whitelist = [process.env.DOMAIN]

const corsOptions = {

    origin: (origin, cb) => {
        const originIsWhitelisted = whitelist.includes(origin)
        cb(null, originIsWhitelisted)
    },
    credentials: true
}
1

There are 1 best solutions below

0
On

You probably need to handle CORS preflight requests, which your browser presents as OPTION requests before the POST requests.

Add this line to your code.

router.options('/upload', cors())