Is it possible to ignore params?
Could you give me an example?
For example ignoring query params in a GET request, e.g.
/apps?sig=1723718230123&blah=1231231234
so when you are working with external clients that make calls, you may wanna ignore these query params?
Thanks for the example. If you use the project as a module, you can ignore params this way:
var jsonServer = require('json-server')
var server = jsonServer.create()
server.use(function (req, res, next) {
delete req.query.sig
delete req.query.blah
next()
})
server.use(jsonServer.defaults)
server.use(jsonServer.router('db.json'))
server.listen(3000)
While this is an acceptable solution, it'd almost be nicer to have some sort of config.json file that allowed for quick configs like this.
I've updated code to automatically ignore query params for which there's no corresponding path in the collection.
For example:
# Before
GET /posts?author=typicode&foo=bar # []
# After
GET /posts?author=typicode&foo=bar # [{...}, {...}]
It's not yet released as I plan to tweak some other things before.
As for a config.json, I think it's a good idea. But the hard part is to distinguish between common or specific cases. So feedback in general is very helpful :)
Just released v0.8.0 with the "auto-ignore" feature.
Does the above solution work with custom routes? I've simple app below and its not working for me. I want to ignore query params in my request.
var jsonServer = require('json-server');
var server = jsonServer.create();
server.use(function (req, res, next) {
delete req.query.sig;
delete req.query.blah;
next();
})
var router = jsonServer.router('db.json');
var middlewares = jsonServer.defaults();
server.use(middlewares);
server.use(jsonServer.rewriter({
,"/app/getEmployees": "/employees"
}));
server.use(router);
server.listen(3000, function(){
console.log('server is running');
});
If I make a request like http://localhost:3000/app/getEmployees?sig=abc&blah=xyz it doesn't fetch the employees. It only works without query params like when I make request http://localhost:3000/app/getEmployees.
@rdhanai I with same problem here
Boa tarde, existe algum link com as configura莽玫es que eu posso utilizar no config.json estou tentando utilizar o auto-ignore e n茫o estou conseguindo
The auto-ignore isn't working for me either with custom routes, but if you only have a few params, you can anticipate them in your routes.
"/myRoute/entity/:id?param1=:param1¶m2=:param2": "/newRoute/:id/"
~Note that it appears (18 Oct 2019) that the params must appear in the same order as they're listed in your custom route. Not ideal.~ Need to test more -- now if I only have one param in the route, they're all ignored, which isn't bad.
Not great, and I should get the module idea working, but an okay kludge.
I know that the thread is old, but nevertheless there is another way which works for me.
You can create custom route inside routes.json like this
{
"/apps\\?*": "/apps?"
}
This will ignore all query parameters.
Most helpful comment
I know that the thread is old, but nevertheless there is another way which works for me.
You can create custom route inside
routes.jsonlike this{ "/apps\\?*": "/apps?" }This will ignore all query parameters.