json-server icon indicating copy to clipboard operation
json-server copied to clipboard

Customize json-server utilities

Open ev45ive opened this issue 11 years ago • 5 comments

Is is possible to customise requests and responses for json-server easily?

I mean for example:

  • change names of _start _end or filtering, etc. ?
  • change or envelope response to include some metadata, etc?

Mainly I was thinking of using json-server to mock fast HATEOAS-like APIs.

ev45ive avatar May 19 '15 19:05 ev45ive

No, sorry.

Changing names would be an easy feature to add though.

But, the problem with meta-data is mostly that I don't know what to include. I'm also more familiar with "classic" REST than hypermedia.

For example with the following db.json:

{
  "posts": [
    { "id": 1, "title": "json-server", "author": "typicode" }
  ],
  "comments": [
    { "id": 1, "body": "some comment", "postId": 1 }
  ]
}

What kind of meta would you need and for which route? I can understand that for pagination it would help, but besides that? If you have the time to describe your case, I would be interested.

typicode avatar May 20 '15 22:05 typicode

Well, I did a small test adding hal module. Getting the resource and adding some HAL metadata before sending the response. For example: router.js:

var hal = require('hal');

// GET /:resource/:id
    function show(req, res, next) {
        var resource = db(req.params.resource)
            .get(utils.toNative(req.params.id))
        var halresource = new hal.Resource(resource, '/' + req.params.resource + '/' + req.params.id);
        if (resource) {
            res.jsonp(halresource)
        } else {
            res.status(404).jsonp({})
        }
    }

And I'm getting for /posts/1:

{
  "_links": {
    "self": {
      "href": "/posts/1"
    }
  },
  "id": 1,
  "title": "json-server",
  "author": "typicode"
}

The same approach can be used with the list function for returning the array into the _embedded HAL tag,

juvan avatar Jul 22 '15 14:07 juvan

Hi , Could you please be more specific about "how to change names like _start, _end or _like? Is it possible, I read as yes from comment here above, but when/where to do it? Huge thanks in advance

sedina avatar Oct 13 '16 19:10 sedina

Hi @sedina,

I've not tried it but I think it's possible to do it using JSON Server as a module:

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

server.use(middlewares)

server.use(function (req, res, next) {
  // req.query contains all the query parameters
  // if the URL is something like http://localhost:3000/posts?_myStart=1
  // the code below will change _myStart to the corresponding JSON Server query param
  // dont't forget to delete your custom query param
  if (req.query._myStart) {
    req.query._start = delete req.query._myStart
    delete req.query._myStart
  }
  next()
})

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

typicode avatar Oct 14 '16 11:10 typicode

Huge thanks, this was my guess as well, good enough solution

sedina avatar Oct 14 '16 11:10 sedina