簡體   English   中英

如何在不指定整個路線的情況下刪除快遞

[英]How to delete on express without specificate the whole route

我在快車道上有這樣的路線

app.delete('/items/:id', function (req,res) {
...
})

當我嘗試通過以下方式發送刪除請求時:

http:// localhost / items / 10我成功刪除了該項目。

但是,如果我嘗試將刪除內容發送到http:// localhost / items ,則會顯示Cannot DELETE / items

未指定ID時為什么無法訪問app.delete?

未指定ID時為什么無法訪問app.delete?

因為如果需要,您需要告訴Express id可選的 ,就像這樣:

app.delete('/items/:id?', function (req,res) {
...
})

這將符合以下要求:

  • DELETE /items/123 (其中req.params.id將為'123'
  • DELETE /items/ (其中req.params.id將是undefined
  • DELETE /items (其中req.params.id也將是undefined

由於在快速路由機制中request處理POSTGETPUTDELETE ,因此它期望server的資源。

首次定義路線時

`http://localhost/items/10`  it matches the request with your route that expect

參數,然后找到資源並將其刪除

與其他http://localhost/items相比,它假定資源不同,並且沒有執行或訪問該資源

暫無
暫無

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

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