Comments (7)
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.
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.
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.
Great thanks for your help. I think that will work.
from json-server.
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.
Not sure if you get notified on closed issues, so ping... @typicode
from json-server.
Thank you for the ping, I'll reopen it just not to forget about it
from json-server.
Related Issues (20)
- conditional operators doesn't work HOT 1
- I have a problem opening Json-server --watch.db.json HOT 3
- ?_page= or &_page= no longer working. HOT 1
- _sort=price&_order=asc no longer working HOT 2
- it doesn't work on xxx.js or remote url,though xxx.json is ok
- SyntaxError: Unexpected token '||=' HOT 1
- Please, bring back the `--delay` option HOT 2
- CORS Issue (Access-Control-Allow-Headers)
- _sort=id&_order=desc do not work (--version 1.0.0-beta.0) HOT 1
- paginate should return empty data when there have a bigger `_page`, but return last page data now
- `_like` doesn't work anymore? HOT 1
- _dependent value error
- Licensing HOT 1
- Compressed Uploads
- Getting nested resources doesn't work HOT 1
- cant find package json-server within node_modules HOT 3
- delay [ms] is not working for me...... HOT 2
- I can not not increased by 1 for id HOT 1
- Newly added data is not recognized when trying to update it seconds later HOT 1
- multiple files with beta 1.0.0v1 HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from json-server.