The problem is this. I have a client on flutter that sends different requests to the server at the same static const String baseUrl = 'https://example.store'
. I connected the server itself to this domain. Here is the code of the server itself
const express = require('express')
const mysql = require('mysql')
const cors = require('cors')
const https = require('https')
const socketIo = require('socket.io')
const bcrypt = require('bcrypt')
const { v4: uuidv4 } = require('uuid')
const bodyParser = require('body-parser')
const fs = require('fs')
const app = express()
const server = https.createServer(
{
key: fs.readFileSync(path.join(__dirname, 'cert', 'key.pem')),
cert: fs.readFileSync(path.join(__dirname, 'cert', 'cert.pem')),
},
app
)
const io = socketIo(server)
const port = 3000
app.use(cors())
app.use(express.json())
app.use((req, res, next) => {
res.setHeader('Access-Control-Allow-Origin', '*')
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE')
res.setHeader('Access-Control-Allow-Headers', 'Content-Type, Authorization')
next()
})
In the browser, when sending a request, it outputs the following Access to XMLHttpRequest at 'https://example.store/userlogin' from origin 'https://example.com' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.
I have already looked at many solutions to fix this error and none of them helped. I tried changing the headers, adding
var corsOptions = {
origin: 'http://example.com',
optionsSuccessStatus: 200
Noukin is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.