Json-server: can json-server return xml type?

Created on 9 May 2016  路  2Comments  路  Source: typicode/json-server

Most helpful comment

Yes, simply use the project as a module and modify router.render to output XML. If you look for "json to xml" on npm, you can find some libraries to help you convert data to XML.

You may need to test if data is an array though and put it into an object, since most converter requires objects to be passed.

var jsonServer = require('json-server')
var server = jsonServer.create()
var router = jsonServer.router('db.json')
var middlewares = jsonServer.defaults()

server.use(middlewares)
server.use(router)

router.render = function (req, res) {
  var data = res.locals.data
  var str = convertDataToXML(data)
  res.send(str)
}

server.listen(3000, function () {
  console.log('JSON Server is running')
})

PS: sorry for not suggesting a particular XML converter, it's just that I haven't tried them.

All 2 comments

Yes, simply use the project as a module and modify router.render to output XML. If you look for "json to xml" on npm, you can find some libraries to help you convert data to XML.

You may need to test if data is an array though and put it into an object, since most converter requires objects to be passed.

var jsonServer = require('json-server')
var server = jsonServer.create()
var router = jsonServer.router('db.json')
var middlewares = jsonServer.defaults()

server.use(middlewares)
server.use(router)

router.render = function (req, res) {
  var data = res.locals.data
  var str = convertDataToXML(data)
  res.send(str)
}

server.listen(3000, function () {
  console.log('JSON Server is running')
})

PS: sorry for not suggesting a particular XML converter, it's just that I haven't tried them.

Where is the code for "convertDataToXML"?
If I post an xml data, how to change the code?

Was this page helpful?
0 / 5 - 0 ratings