Search code examples
javascriptmeteoriron-router

How to generate XML files in Meteor using Iron Router


I need to generate a simple XML file to call with Twilio.

Currently, I am trying to output this at a particular route:

<?xml version="1.0" encoding="UTF-8"?>
<Response>
    <Say voice="woman" language="en">Hello, world!</Say>
</Response>

I plan on making it dynamic later and hence, cannot place it with the other asset files.

In my routes file, I am not sure what to do with this. I cannot place this in a template because that gives an error for obvious reasons.

// Twilio voice call TwiML
Router.route('/twilio/my_twiml.xml', {
    // ??
});

Solution

  • With the help of this answer, I was able to get this to work by:

    Router.route('/twilio/my_twiml.xml', {
      where: 'server',
      action: function() {
    
        var xmlData = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
        xmlData += "<Response>";
        xmlData += "<Say voice=\"woman\" language=\"en\">Hello!</Say>";
        xmlData += "</Response>";
    
        this.response.writeHead(200, {'Content-Type': 'application/xml'});
        this.response.end(xmlData);
      }
    });
    

    Note that this is a server-side route.