How can I respond in XML using ExpressJS?

后端 未结 2 1295
遥遥无期
遥遥无期 2020-12-13 18:44

I have a simple code that gives a JSON response for a specific route. Here\'s my current code:

var express = require(\'express\')
  , async = require(\'async         


        
相关标签:
2条回答
  • 2020-12-13 18:57

    You can use any number of the XML libraries available on npm. Here's an example using the simply-named "xml" library:

    var xml = require('xml');
    
    response.set('Content-Type', 'text/xml');
    response.send(xml(name_of_restaurants));
    

    See the module's documentation for a description of how it converts JavaScript objects to XML. If you need things returned in a specific XML format, you'll have more work to do, of course.

    0 讨论(0)
  • 2020-12-13 19:02

    As an update to this, it looks like res.type should be used instead as res.set does not give the same results.

    res.type('application/xml');
    

    More information can be found in the API reference.

    0 讨论(0)
提交回复
热议问题