score:2
Since you're using webpack-dev-server
you can use the proxy
option DevServerProxy.
Your configuration will look like this:
// webpack.config.js
devServer: {
proxy: {
'/internal': 'http://localhost:1337'
}
}
Since I can't see your express
routes on your question I'm speculating about the proxy route if your API
lives on /internal
endpoint then you should modify your React code like this:
const response = await fetch('/internal/provider/check_email_exist', {
method: 'POST',
headers,
body,
})
As you can see I ommited the https://localhost:1337
because the proxy
option from webpack-dev-server
will handle this and it will redirect to http://localhost:1337
. Hope this will help you. Cheers, sigfried.
EDIT
As the comment on your question pointed out you should set the headers on your express
server, not the client, for this task you can use the cors-middleware package.
score:0
Maybe this can help if you face with preflight errors.
My full config:
const cors = require('cors');
const express = require('express');
const { createProxyMiddleware: proxy } = require('http-proxy-middleware');
...
const logLevel = 'info';
const ip = require('ip').address();
const proxyOptions = {
xfwd: true,
target,
changeOrigin: true,
logLevel,
cookieDomainRewrite: {
'*': 'localhost',
},
headers: {
'X-Forwarded-For': ip,
'X-Node': 'true',
},
};
const backNginxApp = express();
backNginxApp.use(
cors({
methods: 'GET,HEAD,PUT,PATCH,POST,DELETE',
origin: 'http://localhost:3000',
optionsSuccessStatus: 200,
credentials: true,
})
);
backNginxApp.use('/api', proxy(proxyOptions));
API: const target = 'https://someapi.com'
Local development running at: http://localhost:3000
Source: stackoverflow.com
Related Query
- CORS issue in codeigniter 4: Response to preflight request doesn't pass access control check
- How to fix "Response to preflight request doesn't pass access control check: It does not have HTTP ok status" error in react app with nodejs api
- tried to access the data via axios but Response to preflight request doesn't pass access control check
- React + ASP.Net Core 3: CORS Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header
- React Express Fetch Post CORS error: Response to preflight request doesn't pass access control check: It does not have HTTP ok status
- CORS issue - Response to preflight request doesn't pass access control check:
- Fetch in ReactJS with Basic Auth return 401 (Unauthorized). Preflight request doesn't pass access control check
- API GATEWAY - Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' when calling api from local host
- response to preflight request doesn't pass access control check: No 'Access-control-Allow-Origin' header is present in the requested resource
- Response to preflight request doesn't pass access control check: It does not have HTTP ok status - React issue
- How to fix CORS error: request doesn't pass access control check?
- Preflight request doesn't pass access control check: It does not have HTTP ok status
- Preflight Request doesn't pass access control
- How to call a react hook fetch request in a functional component to access data then pass to a class component to map?
- Preflight Doesn't Pass Control Check Access-Control-Allow-Origin Not Present
- How do I pass a parameter to API request 2, which I receive from API response 1 in react
- How to fix get request and response with whole html
- How can I fix : Request header field x-access-token is not allowed by Access-Control-Allow-Headers in preflight response?
- How to pass Token of the first request response to the second request header, using Axios in an Ionic React app (2 Post Requests)
- How to fix 431 Request Header Fields Too Large in React-Redux app
- How to get a downloadable file from a readableStream response in a fetch request
- how to pass object as params in Router.push() in nextjs and access that object in other component
- How do I fix a network error received from axios response reactjs
- Request header field … is not allowed by Access-Control-Allow-Headers in preflight response
- Firebase - Request header field x-firebase-gmpid is not allowed by Access-Control-Allow-Headers in preflight response
- How to access values from React Select and pass them to Formik values on Submit?
- Request header field <field-name> is not allowed by Access-Control-Allow-Headers in preflight response
- How to pass Request cookies through node-fetch in isomorphic app?
- How to fix random http request (404 response) from Axios
- React how can you pass a variable to a get request
More Query from same tag
- Custom Overlays for Google Maps v3 using React
- ReactJS — problems with detecting changes in state
- Adding MUI Component to Component Library results in Error "Hooks can only be called inside of the body of a functional component"
- npm install @material-ui/core got error: Unexpected end of JSON input while parsing
- React hook, variable not captured in callback
- React, how can i change the button colors separately in every buttons?
- How can I trigger a function when one of my state changes?
- ReactJS onclick of a button display mongo collection in table format
- Conditional rendering after new data available in props
- function argument in component's constructor in react.js
- JavaScript object function in react component class
- React input or material-ui TextField not working inside material-ui TreeView
- How to change Redux state from a path string?
- Unit test Redux async actions
- File Upload from API Routes Next.js not working
- Export multiples modules in next.config.js : next-images
- Return Zero if input value is NaN
- How to persist the value of state variables after window reloads in nextJS?
- Why does my MERN / Passport User Login not redirect on success?
- How to write js functions server side in nextjs
- Material ui chip with the converted date inside
- How can I using state instead of setTimeout?
- How to get input value upon button click
- Setting/Clearing input in Redux/React
- Can't capture onChange event
- Loading gif fades out before the image completely loads, why is that?
- React component not re-rendering after using UseState Hook
- React Promise: TypeError: Cannot read property 'then' of undefined
- getting error Dropdown `value` must be an array when `multiple` is set
- Pagination should not be rendered in every view in ReactJS?