typicode / json-server

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

Can I add custom route with query by using server.get()? #939

Open imageslr opened 5 years ago

imageslr commented 5 years ago

Hello, I wonder to know how can I return different data based on query parameters. I want it to look like this:

server.get('/books?isbn=:isbn', (req, res) => {
  // ... mock some data
  res.jsonp(req.query)
})

But it seems not work. If I use a rewriter:

const rewriter = jsonServer.rewriter({
  "/books?isbn=:isbn": "/books/1"
})
server.use(rewriter)

It works. But it can only return same data. So could you please tell me how to implement this? Thank you sou much!

joshuaswilcox commented 5 years ago

I have the same issue, i get a 404 with

server.get('/api/thing/another_place?emails=:emailList', (req, res) => {
  res.jsonp(mock data from another module)
});