简体   繁体   中英

how do I allow a google app engine node js script to call cross domain?

I am new to GAE / NODEJS. There is plenty of advice to enable calls hosted by GAE to be called cross domain, but I am having trouble getting my node js app to call another domains API. Is there something I need to do to get GAE to allow cross domain calls? My code is:

app.get('/listProducts', (req, res) => {
    request.get('https://[cross domain]/api/2.0/products', function (error, response, body) {
  console.log('error:', error); // Print the error if one occurred
  console.log('statusCode:', response && response.statusCode); // Print the response status code if a response was received
  console.log('body:', body); // Print the HTML for the Google homepage.
}).auth(null, null, true, '[key goes here]');

You can allow configuring your app.yaml file or setting in your http header. Here the doc (looking for CORS Support )

Follow the example:

You could have a game app mygame.appspot.com that accesses assets hosted by myassets.appspot.com . However, if mygame attempts to make a JavaScript XMLHttpRequest to myassets, it will not succeed unless the handler for myassets returns an Access-Control-Allow-Origin : response header containing the value http://mygame.appspot.com .

Here is how you would make your static file handler return that required response header value:

handlers:
- url: /images
  static_dir: static/images
  http_headers:
    Access-Control-Allow-Origin: http://mygame.appspot.com
  # ...

Note: if you wanted to allow everyone to access your assets, you could use the wildcard '*', instead of http://mygame.appspot.com .

在做新手-我应该返回一个响应代码,例如response.status(200).send(“ ok”)。end();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM