I am facing cors issue and i am sure it is about cloudinary, the error is:Access to fetch at 'https:
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" }); } });
Answers
-
access the code here:
0 -
and the error:
Access to fetch at 'https://cognizen-backend.vercel.app/upload' from origin 'https://cognizen.vercel.app' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.
0