Steam API Access-Control-Allow-Origin Issue

六眼飞鱼酱① 提交于 2019-11-29 15:54:43

I've read a bit about the error, but it seems to require server side code changes or something.

Yes — by the service you want to read data from. i.e. by Steam.

You can't give yourself permission to read data from other people's sites using your visitors' browsers.

Since you can't alter Steam's servers, you'll need to fetch the data from Steam using your server instead of from the visitors' browsers.

You can easily 'cors' modules. use this command

npm install cors --save.

and add these lines in your app.js

var cors = require('cors');    
app.use(cors());

You don't need to get your code dirty.This is a cleaner way.

remove this

app.use(function (req, res, next) {

  // Website you wish to allow to connect
  res.setHeader('Access-Control-Allow-Origin', 'http://api.steampowered.com');

  // Request methods you wish to allow
  res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');

  // Request headers you wish to allow
  res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type');

  // Set to true if you need the website to include cookies in the requests sent
  // to the API (e.g. in case you use sessions)
  res.setHeader('Access-Control-Allow-Credentials', true);

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