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:
- The REST endpoint youâve set up decodes the XML (using
xml2js
and insert the data into a MongoDB collection. You need to replaceServerSession
with the insert to the collection. - You define a publication on the server which publishes that collection.
- As soon as data is inserted into the collection, any clients which have subscribed to the publication will immediately get that data.
- 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 !