I can't set up a proxy in Vite - vite

I need to set up a proxy in Vite for CORS. I have tried different settings. I keep getting the CORS problem in the browser.
Access to load at 'http://localhost:3000/api/login' from origin 'http://localhost:5173' has been blocked by CORS policy: no 'Access-Control-Allow-Origin' header is present for the requested resource. If an opaque response serves you, set the request mode to 'no-cors' to retrieve the CORS-disabled resource.
I have this setting
proxy: {
'/api': {
target: 'https://localhost:3000',
changeOrigin: true,
secure: false,
}
}

Related

Why is my CORS policy suddenly not allowing access to my server?

So my website, www.carpediction.com started blocking frontend (hosted on Netlify) requests to the backend node/express server (hosted on Heroku), with no code changes to the CORS setup.
Repo: https://github.com/relativelyIntuitive/CarpeDiction/
This is my CORS setup block in the 'server/server.js' file:
if (process.env.NODE_ENV === 'production') {
app.use(cors({ credentials: true, origin:
'https://www.carpediction.com' }));
} else {
app.use(cors({ credentials: true, origin:
'http://localhost:3000' }));
}
This is the error message logged to the console when accessing www.carpediction.com:
"Access to XMLHttpRequest at 'https://carpe-diction.herokuapp.com/api/wotd/latest' from origin 'https://www.carpediction.com' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource."
My question is this: What is the issue with my current CORS policy setup? The origin listed as blocked in the error message is the same allowed-origin specified in the server's CORS setup, and yet the header is missing. No code was changed in my repo that initiated this issue, one day I just noticed my site wasn't working and investigated. My best guess is that some CORS syntax was changed, but I haven't been able to find any documentation of such a change that would break my server.
If there is any better information I can provide, please let me know! Thanks!
-Zack
P.S.
I even tried to allow all origins with CORS by specifying "*" as the origin in the policy. This still did not work. The error message was the same.

How to solve node.JS passport oauth2 cors error

I am trying to configure node passport oauth2 login.
If call the get url directly from the browswer, everyhting works perfectly.
I am trying to set up the login using react client as frontend.
react axios api call
const res = await Axios.get(http://locahost:5000/login`)
node.js express
app.use(cors({
'allowedHeaders': ['sessionId', 'Content-Type', 'authorization'],
'exposedHeaders': ['sessionId','authorization'],
// 'origin': true,
// 'Access-Control-Allow-Origin': 'http://localhost:8080',
'methods': 'GET,HEAD,PUT,PATCH,POST,DELETE, OPTIONS',
'preflightContinue': false
}));
app.use(bodyParser.json({ limit: "50mb", extended: true }));
app.get('/npt/login', passport.authenticate('oauth2'));
This is the error I get
Access to XMLHttpRequest at 'https://...' (redirected from 'http://localhost:5000/npt/login') from origin 'http://localhost:8080' 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.
Do I need to set the headers in the client app?
UPDATE:
I am trying to use this configuration using Azure App Registration.
I have updated the express CORS settings.
UPDATE2:
I have managed to solve the issue running the frontend on the same origin as the nodeJS express server.
This is the example I have followed: https://github.com/cicorias/react-azuread-passport
if you run react side via CRA
you can add proxy in package.json file
like this :
"proxy": "http://yourTarget.com",
and call http requests from
http://localhost:3000
notice : when you change package.json file you must reset npm start command to see the changes

How to Enable CORS for Credentials and Preflight in Node/Express

I know, there are a billion CORS questions on here. I have gone through dozens. I'm still not sure what the issue is, and I think I understand CORS decently well, but obviously I'm missing something here.
I'm trying to get my (Node/Express) API running on a real URL (i.e. deployed, not using origin: "*"), and tell the browser to actually fulfill requests from my GUI's URL.
For some routes, I need authentication, so I need to add credentials in there.
This is a pretty standard use case, but maybe something changed with the API since I last did this or maybe I just forgot, but regardless I can't seem to get this working and I'm not sure what I'm missing.
There seem to be several ways to go about enabling CORS for all requests in my Express app. I'm currently using the cors middleware with its options object.
Specifically, here's the code I'm using.
const app: express.Application = express();
const corsOptions = {
credentials: true,
origin: 'https://my-gui.example.com',
methods: 'GET,HEAD,PUT,PATCH,POST,DELETE,OPTIONS',
preflightContinue: true,
}
app.use(cors(corsOptions));
What am I doing wrong? When I make a fetch request from my browser, I receive the below error.
Access to fetch at 'https://my-api.example.com/graphql' from origin 'https://my-gui.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. If an
opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource
with CORS disabled.
If I add app.options('*', cors(corsOptions)); before the app.use call (why I would need this when I specified OPTIONS in the method list, I don't know), I get a different error (which leads me to believe I do need this for some reason).
Access to fetch at 'https://my-api.example.com/graphql' from origin 'https://my-gui.example.com'
has been blocked by CORS policy: Response to preflight request doesn't pass access control
check: It does not have HTTP ok status.
I think you are trying to access this website https://my-api.example.com/graphql but the shared snip looks like you gave an access only to https://my-gui.example.com and this is different from above one.
So my answer is please change your snippet like below,
const corsOptions = {
credentials: true,
origin: "*",
methods: 'GET,HEAD,PUT,PATCH,POST,DELETE,OPTIONS',
preflightContinue: true,
}
(or) try
const corsOptions = {
credentials: true,
origin: ['https://my-gui.example.com','https://my-api.example.com/graphql'],
methods: 'GET,HEAD,PUT,PATCH,POST,DELETE,OPTIONS',
preflightContinue: true,
}

HTTPrequest using axios results in CORS error

I build my own API which is running on port 5000. Whenever I want to make a request to it , I get the following error:
Access to XMLHttpRequest at 'http://localhost:5000/user' from origin 'http://localhost:3000' 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 already did some research but the only thing that seems to work is a chrome extension, which isnt ideal.
Here's what i did :
const config = {
headers : {
'Content-Type' : 'application/json',
'Access-Control-Allow-Origin': '*'
}
};
Simply, browsers block responses if server does not allow CORS. So if you are using NodeJS at your API, use this library.

Hapi.js with Glue CORS headers not setting

Okay, so this is what my glue manifest looks like. As you can see the cors object is being set but I keep getting this stupid error when running from frontend app: localhost:3000.
Access to XMLHttpRequest at 'http://localhost:8082/api/v1/check_out_order' from origin 'http://localhost:3000' 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.
"glueManifest": {
"server": {
"port": 8082,
"routes": {
"cors": {
"origin": ["*"],
"additionalHeaders": ["cache-control", "x-requested-with"]
}
}
},
anyone that may have a solution for me will be appreciated much! TIA!
try this: var server= new Hapi.Server({ port: 8082, routes: { cors: true }});
Your back-end needs to accept also the 'OPTION' type of HTTP request on all endpoints + add the header to all your responses.
The HTTP OPTIONS method is used to describe the communication options
for the target resource. The client can specify a URL for the OPTIONS
method, or an asterisk (*) to refer to the entire server.
Resource: https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/OPTIONS
The Access-Control-Allow-Origin response header indicates whether the
response can be shared with requesting code from the given origin.
Resource: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Access-Control-Allow-Origin

Resources