-1

I am trying to make a POST request from my localhost to a remote server and I am getting the error "Access to XMLHttpRequest at 'https://chat-app-main-server.vercel.app/api/auth/login' from origin 'http://127.0.0.1:5173' has been blocked by CORS policy." The strange thing is that the code works fine when I run it locally, but it fails when I try to run it on a remote server.

I have tried adding the following headers to my request, but it doesn't seem to help.:

issue image

I am not sure what else to try. How can I troubleshoot this issue and fix it?

  • Have you setup CORS on your express server? Docs: https://expressjs.com/en/resources/middleware/cors.html – HazAnwar Mar 29 '23 at 16:43
  • Yes I've installed cors. and set up it in app.js file: app.use(cors({ origin: 'http://127.0.0.1:5173', methods: ['GET', 'POST', 'PATCH', 'DELETE', 'PUT'], credentials: true })) – Aimun Nahar Mar 29 '23 at 16:57

1 Answers1

-1

Please install CORS npm or yarn package in nodejs(Backend)

If you use your local server then use like:

const express = require('express')
const app = express()
const env = require('dotenv');
const bodyParser = require('body-parser');
const userRoute = require('./routers/users')
const cors = require('cors')
 
env.config()

require('./dbConn/dbConn')

const corsOptions = ["http://localhost:3000/"] // (You can ur server also)
app.use(cors(corsOptions)
Tyler2P
  • 2,324
  • 26
  • 22
  • 31