简体   繁体   中英

Meteor - transfer data from API to Server to Client

I am struggling a bit with Meteor, i have this app that i would like to connect with an API client, which provides me a Secret API key, which i must not publish (in the client).

The thing is when i send the request, i get a JSON data, and i would like to pass this data to the client.

API > Server Call -> Client (Rendering).

But so far i have not come to a solution how can i do this.

I have a basic understanding how Meteor Works, but i have a good knowledge about JavaScript/NodeJS etc.

A little bit of help would really be appreciated.

Thank you.

This sounds like a good use case for a client making a call to a server-side method . The server can then use the secret key to make an HTTP request and send the result back to the client without exposing the key. Please note that your server-method must exist inside of a server directory to avoid inadvertently shipping the key to the client (see Structuring your application ).

client

Meteor.call('getApiResult', function(err, result) {
  if (result) {
    return console.log(result);
  }
});

server

Meteor.methods({
  getApiResult: function() {
    var secret = 'abc123';
    try {
      var result = HTTP.get('http://example.com/', {params: {key: secret}});
      return result.data;
    } catch (_error) {
      return false;
    }
  }
});

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