本文介绍了使用axios调用时如何在Node.Js应用程序中设置X-Forwarded-For的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

好的,我刚刚有机会在Node.js中工作,我正在其中使用axios库从第三方URL中获取数据.

Okay, I just got an opportunity to work in nodejs, where I am using axios library to fetch the data from third party url.

在部署到ENV之前,一切工作都非常顺利.

Everything was working very smoothly until it was deployed to staging ENV.

在使用Rails时遇到类似的问题,发现的解决方案是使用

Similar issue I got while working with rails, and the solution I found was to use

@request['X-Forwarded-For'] = 'XX.YYY.Z.Z'.

现在来看这个问题,已经尝试了

Coming to the issue now, have already tried

options = { headers: { 'X-Forwarded-For': 'XX.YYY.Z.Z' } },即使在本地env:/

options = { headers: { 'X-Forwarded-For': 'XX.YYY.Z.Z' } } and its not working at all even at local env :/

axios.get(URL, options);这就是我使用它来获取数据的方式,但是它一直在不断加载,

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?

推荐答案

从express创建应用程序后使用此中间件

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.

这篇关于使用axios调用时如何在Node.Js应用程序中设置X-Forwarded-For的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-07 00:00