Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
XML to JSON
var util = require('util');
var parser = require('xml2json');
var express = require('express');
var app = express();
app.use(anyBodyParser);
app.post('/xml_to_json', function (req, res) {
var json = parser.toJson(req.rawBody, { arrayNotation: true });
res.send(json);
});
app.post('/xml_to_xml', function (req, res) {
var json = parser.toJson(req.rawBody, { arrayNotation: true });
/*
modify the json here and send it back as XML
*/
var xml = parser.toXml(json);
res.send(xml);
});
var server = app.listen(4000, function () {
var host = server.address().address;
var port = server.address().port;
console.log('Escuchando en http://%s:%s', host, port)
});
function anyBodyParser(req, res, next) {
var data = '';
req.setEncoding('utf8');
req.on('data', function(chunk) {
data += chunk;
});
req.on('end', function() {
req.rawBody = data;
next();
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment