typicode / json-server

Get a full fake REST API with zero coding in less than 30 seconds (seriously)
Other
72.66k stars 7.01k forks source link

Possible to run with namespaces? #984

Open watadarkstar opened 5 years ago

watadarkstar commented 5 years ago

Given the following JSON:

{

  "posts": [
    {
      "id": 1,
      "name": "Bugs Bunny"
    },
    {
      "id": 2,
      "name": "Wile E. Coyote"
    },
    {
      "id": 3,
      "name": "Yosemite Sam"
    }
  ]

}

Is it possible to run json-server such that it responds to /api/posts rather than /posts?

I want to bring this issue back up from the grave because we need it for some use cases for deploying APIs alongside the same domain as a static site: https://github.com/zeit/now-examples/tree/master/json-server

I'd like to work on this feature if it doesn't exist already.

tinavanschelt commented 5 years ago

Hi @watadarkstar, pretty sure this is in the docs: https://github.com/typicode/json-server#add-custom-routes :)

watadarkstar commented 5 years ago

@tinavanschelt Hmmm that works if you want to run json-server from the command line but I'm not sure how to get custom routes to work as an npm module:

I thought maybe this section could be it but I don't think so? https://github.com/typicode/json-server#custom-routes-example

tinavanschelt commented 5 years ago

In that case we're probably working on a similar issue. I'm actually just trying to debug a old-ish express integration that used to work (https://github.com/tinavanschelt/redux-starter-with-api/blob/master/server.js) - think it broke during a recent upgrading of packages.

This might help? https://github.com/typicode/json-server/issues/237#issuecomment-465322762

If not, it's probably a case of the blind leading the blind here ☺️ - just thought your answer might be in the docs.

watadarkstar commented 5 years ago

@tinavanschelt This is exactly what I'm looking for!

const routes = require('./routes.json')
...
server.use(jsonServer.rewriter(routes))

Thank you!