How do I receive XML information from the API on the server side?

I defined a server-side route on the server side, and then how do I handle the XML information that the API sends to this route? Do you need to install any special packages? Please advise.
Thanks in advance !

In order to work with XML in JavaScript, you’ll need the xml2js package, or one of the many alternatives.

First of all, thank you very much for your reply!
I defined a route on the server side to receive the XML information passed by the API, but the data received was not available to the console, and the code was like this:
Router.route(’/wxnotify_url’, function () {
var request = this.request;
var response = this.response;
var data = req.body;
ServerSession.set(“request”,data);
response.end(‘success’);
},{where:‘server’});
I to write the value of access to the server session, then defines a callback function for the session, on the client side to invoke the callback function to obtain the session value, but can’t show the data on the client side of things.

Sorry, only just seen your reply (better to tag me or use the Reply link).

For future reference, if you can put your code between lines with triple-backticks, like this:

```
your
code
here
```

it will make it easier to read.

Do I understand that you want to deliver the data object to the client?

Thank you very much for your reply. I’m very sorry, I am a Chinese. And I’m a meteor primary users, few people use meteor to development projects here. I was the first time in the official BBS posts, I can’t use this BBS very well.
You are right, I really want to send data from the server to the client. But now I don’t know how to convert the received XML information. Even I’m not sure I would receive the API XML information sent to come over.

How do I define a path to receive XML information from the receiving API on the server?

When you say “route” do you mean a Meteor.Method ?

You can make methods to receive the XML information in the form of a string and then convert with the relevant Packages.

//server
Meteor.methods({
  receiveXML(xml) {}
})

//client
Meteor.call('receiveXML", xmlData, (err, res) => {})

No need to apologise!

Which client? In a typical web app there may be many users. Which user should get the data?

Meteor has two ways of ensuring the “right client” gets the “right data”:

  • Meteor methods: the client requests the data from the server.
  • Pub/sub: the client subscribes to data on the server.

I’d suggest doing it this way to begin with:

  1. The REST endpoint you’ve set up decodes the XML (using xml2js and insert the data into a MongoDB collection. You need to replace ServerSession with the insert to the collection.
  2. You define a publication on the server which publishes that collection.
  3. As soon as data is inserted into the collection, any clients which have subscribed to the publication will immediately get that data.
  4. Instead of (or as well as) (3), your clients could just request any data from the collection by using Meteor.call to a method on the server.

If you are expecting a huge client base and/or very high velocity posts to your endpoint, option 4 is more easily scalable than option 3.

EDIT: I have a demo repo which shows XML being converted to JSON and inserted into a MongoDB collection. It does not get it’s data by defining a REST endpoint, so in that respect it’s different from your use case, but the XML handling may be useful:

Thank you very much !

1 Like