Microservices | TechnologyMicroservices architecture principle #6: One team is responsible for full life cycle of a Micro service
Jan Vermeir 02 Jun, 2015
wechat.jsfile, but instead of calling
app.get()to create a handler for GET requests, we will call
app.post()to register a handler for POST requests. As we don’t want to write a lot of XML-parsing boilerplate code ourselves, we will install an XML body parser for Express that can handle this for us. Let’s start with that. Install
body-parser-xmlwith the following command in your terminal: Let’s set up the body parser. In
index.js, change the two top lines (the ones that set up the Express app) so they reflect the following:
This piece of code sets up a POST handler on the router (line 6), takes the parsed XML out of the body and switches over the message type (line 9, the XML parser puts single text nodes into an array so that’s why we need to access it with index 0). The function then forwards the response object and parsed XML to one of three handlers (line 11, 13 and 15), for handling new events, text messages and any messages of unknown type respectively. The last step will involve implementing these handlers and returning an XML response that will be sent to the user.
handleEventfunction extracts the event type from the XML data and checks if it is a
subscribeevent. It then creates an XML response by flipping the sender and receiver from the XML data and adding a string message. This will welcome new followers with the ‘Welcome to our Official Account!’ message. While we’re at it, let’s implement the other message handlers: In the
handleTextfunction, the string that is sent back is the reversed text message sent by the user. In the
handleUnknownfunction, we just send back a message telling the user that our echo service does not (yet) understand what they’re saying. And that’s it! After all the hard work it is time to see what happens when we start to follow our own Official Account and send some messages. To start following yourself, scan the QR code that is shown on the WeChat sandbox page, halfway down. On your phone, tab the ‘+’ at the top-right corner in the chat overview, then tap ‘Scan QR code’. After scanning the code, you should see a simple page with a large ‘Follow’ button. If you tap that, WeChat should open a new chat with the echo service. It should welcome you with the message we added earlier. If you send a text message, the echo service should reply with your message reversed. If you send anything else (a picture maybe, or your location) the echo service will tell you that it doesn’t understand that message type. If this all works, good job!