I am facing cors issue and i am sure it is about cloudinary, the error is:Access to fetch at 'https:

zeeshan
zeeshan Member Posts: 3
edited May 28 in Developer APIs

import express from 'express';

import mongoose from 'mongoose';

import cors from 'cors';

import Article from './Schema/Article.js'

const app = express();

import fileUpload from 'express-fileupload';

import { v2 as cloudinary } from 'cloudinary';

import { config as dotenvConfig } from 'dotenv';

    dotenvConfig(); 

// Configure Cloudinary 

  cloudinary.config({     

  cloud_name: process.env.CLOUD,        api_key: process.env.API_KEY,        api_secret: process.env.API_SECRET,        secure: true // Ensures secure URLs are used    });


    import Jwt from 'jsonwebtoken';


    const jwtkey = 'cognizen';    app.use(express.json());


    const corsOptions = {        origin: '*',        optionsSuccessStatus: 200,        credential:true,        methods: ["GET", "POST", "PUT", "DELETE"],        allowedHeaders: ["Content-Type", "Authorization"]    };


    app.use(cors(corsOptions));


    app.use(fileUpload({        useTempFiles: true    }));

app.post('/upload', async (req, res) => {       

const file = req.files.photo;     

  try {           

const result = await cloudinary.uploader.upload(file.tempFilePath);           

const Item = getItemModel(req.body.type);
            const newMember = new Item({             

  ...req.body,                photo: result.secure_url,             id: generateRandomCode()       })


            await newMember.save();   

        res.status(200).send({ message: "Image uploaded and member created successfully" }); 

      }

catch (error) {     

      console.error("Error uploading image and creating member:", error);            res.status(500).send({ message: "Something went wrong" });        }    });

Tagged:

Answers