问题
Okay, I just got an opportunity to work in nodejs, where I am using axios
library to fetch the data from third party url.
Everything was working very smoothly until it was deployed to staging ENV.
Similar issue I got while working with rails, and the solution I found was to use
@request['X-Forwarded-For'] = 'XX.YYY.Z.Z'
in header.
Coming to the issue now, have already tried
options = { headers: { 'X-Forwarded-For': 'XX.YYY.Z.Z' } }
and its not working at all even at local env :/
axios.get(URL, options);
This is how I am using it to fetch the data, but it keeps loading and loading,
Is there anything I am missing or alternative of it so that I can try that instead?
回答1:
use this middleware after creation of app from express
//CORS error solution
app.use((req,res,next)=>{
res.setHeader('Access-Control-Allow-Origin','http://localhost:3000');//your frontend origin
res.setHeader('Access-Control-Allow-Credentials', true);
res.setHeader('Access-Control-Allow-Methods','GET,POST,PUT,DELETE,PATCH');
res.setHeader('Access-Control-Allow-Headers','Content-Type ,Origin, X-Requested-With, Accept, Authorization');
next();
})
Hope this will help you. if you have any query then let me know.
来源:https://stackoverflow.com/questions/58688647/how-to-set-x-forwarded-for-in-node-js-app-while-calling-with-axios