[英]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
處理POST , GET , PUT , DELETE ,因此它期望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.