How to set X-Forwarded-For in Node.Js app while calling with axios

烈酒焚心 提交于 2020-01-25 06:45:13

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!