Giter Club home page Giter Club logo

Comments (7)

typicode avatar typicode commented on September 12, 2024 1

Hi Chuck,

You can nest resources up to one level. For example:

// nested resources
GET /posts/1/comments

But you can filter them too

// Filters
GET /comments?postId=1
GET /users?member=true&status=public

You can also mount JSON Server on an Express app

var one = jsonServer.router('db1.json')
var two = jsonServer.router('db2.json')

var app = express()

app.use('/some/route', one)
app.use('/other/route', two)

Also if you want to provide some informations why you need this kind of routes (without :id params) that would help me understand better your use case.
I mean, personally, I would do GET /users/:userId/emails?

from json-server.

typicode avatar typicode commented on September 12, 2024 1

Well, can't say you'll be able to mimic all the behaviors since JSON Server is about some defaults.

But you can do this:

var v1 = jsonServer.router('v1.json')
var v2 = jsonServer.router('v2.json')

// It would be also possible I think to overwrite some routes

var server = jsonServer.create()
server.use('/v1', v1)
server.use('/v2', v2)
server.listen(3000)

And in v2.json, simply put:

{
  "users": [ ... ]
}

Then GET /v2/users should work.

If you need more flexibility, I would suggest Express + lowdb or projects like dyson or interfake.

Hope it will help :)

from json-server.

termleech avatar termleech commented on September 12, 2024

Yea unfortunately I'm trying to use this to mock and existing api which has endpoints like

/api/v1/email
/api/v2/users

I'm using connect-proxy to proxy requests from /api to the json-server and was hoping to be able to do "levels" like that. Worst case I can just do multiple proxies to multiple json servers. I tried something like:

{
  "v2/users": [
     {id: 1, username: 'test' }
  ]
}

Which when I looked at the log I saw

``
http://0.0.0.0:9002/v2/users


but a request to /v2/users never returned anything.  I may just try either use the express route or just multiple proxy unless you think it's something easy that could be added.

from json-server.

termleech avatar termleech commented on September 12, 2024

Great thanks for your help. I think that will work.

from json-server.

danielcrisp avatar danielcrisp commented on September 12, 2024

I realise this is more of a workaround, but when I use the method described above the Congrats! You're successfully running JSON Server page only lists the endpoints defined in the last router, e.g. v2.json

It looks like the low database is clobbered with each new router definition:

if (_.isObject(source)) {
    db = low()
    db.object = source     // <--- previous routes are overwritten here
} else {
    db = low(source)
}

https://github.com/typicode/json-server/blob/master/src/server/router/index.js#L26

Do you think it'd be possible, in theory, to change this so /api/db/ lists all routes?

from json-server.

danielcrisp avatar danielcrisp commented on September 12, 2024

Not sure if you get notified on closed issues, so ping... @typicode

from json-server.

typicode avatar typicode commented on September 12, 2024

Thank you for the ping, I'll reopen it just not to forget about it

from json-server.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.