簡體   English   中英

在Restify中如何支持可選參數?

[英]How to support optional parameters in restify?

我正在使用Restify創建REST API。 我已經以/ ConfigDetails /:name /:id這樣的格式創建了路由,其中​​name和id是可選的。

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;

我的問題是,如何使它們可選? 使用我定義的當前路由,除非同時存在所有兩個參數,否則它將無法解析,並且會屬於默認路由。 根據我的理解,重新調整不支持'?' 誰可以幫我這個事。

我認為您不能在整頓中做到這一點。 我有一份工作要為您服務。

  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
  }

至於REST API 最佳做法 ,您不應放置可選的路徑參數。 對於非GET請求,它應該在查詢參數中或在請求正文中。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM