0

I am using Restify for creating REST APIs. I have created the route in such format /ConfigDetails/:name/:id where name and id are optional.

If none of the two params is given, all configdetails will be fetched;
If name is given, config details for that name will be returned;
If id is given, config details for that id will be returned;

My question is, how do I make them optional? With the current route I've defined, unless all two parameters are present, it will not be able to be resolved and will fall into the default route. As per my understanding restify don't support '?' Can anyone help me on this.

Aswad
  • 29
  • 2
  • 5

1 Answers1

0

I think you can't do this in restify. I have a work around which my work for you.

  router.get('/ConfigDetails/:name/:id', handler)
  router.get('/ConfigDetails/:name', handler)
  router.get('/ConfigDetails', handler)


  function handler(req, res, next) {
     const {name = '', id = ''} = req.params
     // your code here
  }

As for REST API best practices, you should not put optional path parameters. It should be either in query params or request body for non GET requests.

Jasper Bernales
  • 1,601
  • 1
  • 11
  • 16