Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I respond in XML using ExpressJS?

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')
  , http = require('http')
  , mysql = require('mysql');

var app = express();

var connection = mysql.createConnection({
    host: 'localhost',
    user: '****',
    password: "****",
    database: 'restaurants'
});

connection.connect();

// all environments
app.set('port', process.env.PORT || 1235);
app.use(express.static(__dirname + '/public/images'));


app.get('/DescriptionSortedRating/',function(request,response){
    var name_of_restaurants;

    async.series( [
        // Get the first table contents
        function ( callback ) {
            connection.query('SELECT * FROM restaurants ORDER BY restaurantRATING', function(err, rows, fields)
                {
                        console.log('Connection result error '+err);
                        name_of_restaurants = rows;
                        callback();
                });

 }
   // Send the response
], function ( error, results ) {
    response.json({
        'restaurants' : name_of_restaurants
    });
} );

} );



http.createServer(app).listen(app.get('port'), function(){
  console.log('Express server listening on port ' + app.get('port'));
});

How can I make an XML response equivalent to the JSON above?

like image 923
Devrath Avatar asked Jan 28 '14 06:01

Devrath


People also ask

What is response XML?

responseXML read-only property returns a Document containing the HTML or XML retrieved by the request; or null if the request was unsuccessful, has not yet been sent, or if the data can't be parsed as XML or HTML. Note: The name responseXML is an artifact of this property's history; it works for both HTML and XML.

How do I send XML data to a restful web service?

If you want to send XML data to the server, set the Request Header correctly to be read by the sever as XML. xmlhttp. setRequestHeader('Content-Type', 'text/xml'); Use the send() method to send the request, along with any XML data.

Which method sends response data in Express?

Introduction. In this article, you will learn about the res object in Express. Short for response , the res object is one half of the request and response cycle to send data from the server to the client-side through HTTP requests.

What is the response object in Express?

The response object specifies the HTTP response when an Express app gets an HTTP request. The response is sent back to the client browser and allows you to set new cookies value that will write to the client browser.


2 Answers

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.

like image 161
Ethan Brown Avatar answered Oct 19 '22 19:10

Ethan Brown


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.

like image 28
Christopher Dyke Avatar answered Oct 19 '22 21:10

Christopher Dyke