使用expo到express服务器时没有请求头来源

tot*_*oob 5 express react-native expo

我正在学习 React Native,并且正在我的手机上通过 expo 对其进行测试,并且我正在尝试远程调用一个 api,该 api 在 Web 服务器以及本地主机上与我的 React 包完美配合。

我的应用程序.js

var allowedOrigins = ['http:// localhost:3000', 'http:// site.com'];

app.use(function(req, res, next) {
    const origin = req.headers.origin;
    res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE');
    res.header('Access-Control-Allow-Credentials', true);
    if(allowedOrigins.indexOf(origin) > -1){
        res.setHeader('Access-Control-Allow-Origin', origin);
    }
    res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept, Authorizati$
    next();
});
Run Code Online (Sandbox Code Playgroud)

问题是,当我尝试从应用程序调用 api 时,请求标头中没有来源。

App.js(反应本机)

class User extends React.Component {
    render() {

        fetch(`https://api.site.com/user`)
        .then((response) => { console.log('response', response) })
        .catch((data) => console.log('data', data))

        return null
    }
}
Run Code Online (Sandbox Code Playgroud)

从博览会访问时的 req.headers 看起来像

{ connection: 'upgrade',
  host: 'api.site.com',
  'if-none-match': 'W/"19f-pkIEHBJ9V3GhwsEXhmyI2oBhsbo"',
  accept: '*/*',
  'user-agent': 'Expo/2.6.7.1015752 CFNetwork/889.9 Darwin/17.2.0',
  'accept-language': 'en-us',
  'accept-encoding': 'br, gzip, deflate' }
Run Code Online (Sandbox Code Playgroud)

我是否错过了学习 React for Web 和 React Native for Apps 之间的某个步骤?

riw*_*iwu 4

来源是发出请求的网站的 URL,因此它不适用于移动应用程序。

无需为移动应用程序设置CORS标头,因为只有浏览器限制跨源请求。

如果您的移动应用程序无法向服务器发出请求,则问题出在其他地方。