Request header field Authorization is not allowed by Access-Control-Allow-Headers in preflight response. (node, react, axios)

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



Request header field Authorization is not allowed by Access-Control-Allow-Headers in preflight response. (node, react, axios)



This is in my server.js:


app.use(helmet())
app.options('*', cors())
app.use(function (req, res, next)
res.setHeader('Access-Control-Allow-Origin', '*')
res.setHeader('Access-Control-Allow-Methods', 'GET,POST,PUT,DELETE,OPTIONS')
res.setHeader('Access-Control-Allow-Headers', 'Content-Type,Cache-Control')
if (req.method === 'OPTIONS')
res.sendStatus(200)
else
next()

)



I have a basic node/webpack/react situation. I am making my request in the client with axios. I know that the request is solid, because I have tested it in isolation. I have also tried every combination of Access-Control-Allow-xxxxx that I have found/can think of. It has to be something with this middleware. Any help/insights would be greatly appreciated!


Access-Control-Allow-xxxxx





What happens when you add Authorization to your Access-Control-Allow-Headers?
– Brad
Aug 13 at 4:40


Authorization


Access-Control-Allow-Headers





Instead of res.setHeader try res.header.
– David R
Aug 13 at 4:42


res.setHeader


res.header




1 Answer
1



I assume you are trying to make a api call from your client to the express/node server, yes? Have you tried using a proxy?



try setting the port of your server to something like localhost:5000. Then try


app.use(cors())



instead of


app.options('*', cors())



then back in your client side in your React package.json simply add a proxy


},
"proxy": "http://localhost:5000"



or whatever port your server is on.



That should solve your issue.






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

Creating a leaderboard in HTML/JS