[英]How to delete on express without specificate the whole route
I have a route like this on express 我在快车道上有这样的路线
app.delete('/items/:id', function (req,res) {
...
})
When I try to send a delete request on: 当我尝试通过以下方式发送删除请求时:
http://localhost/items/10 I delete the item successfully. http:// localhost / items / 10我成功删除了该项目。
However, if I try to send the delete to http://localhost/items I get Cannot DELETE /items 但是,如果我尝试将删除内容发送到http:// localhost / items ,则会显示Cannot DELETE / items
Why can't I access to app.delete when I don't specify the id? 未指定ID时为什么无法访问app.delete?
Why can't I access to app.delete when I don't specify the id?
未指定ID时为什么无法访问app.delete?
Because if you want that, you need to tell Express that the id
is optional , like this: 因为如果需要,您需要告诉Express
id
是可选的 ,就像这样:
app.delete('/items/:id?', function (req,res) {
...
})
This will match the following requests: 这将符合以下要求:
DELETE /items/123
(where req.params.id
will be '123'
) DELETE /items/123
(其中req.params.id
将为'123'
) DELETE /items/
(where req.params.id
will be undefined
) DELETE /items/
(其中req.params.id
将是undefined
) DELETE /items
(where req.params.id
will also be undefined
) DELETE /items
(其中req.params.id
也将是undefined
) As in express routing mechanism request
is deals for POST , GET , PUT , DELETE ,it expects the resource in your server
. 由于在快速路由机制中
request
处理POST , GET , PUT , DELETE ,因此它期望server
的资源。
when you first define route 首次定义路线时
`http://localhost/items/10` it matches the request with your route that expect
parameter then it find the resource and delete it 参数,然后找到资源并将其删除
As compared to other http://localhost/items
It assume different resource and not perform or hitting that resource 与其他
http://localhost/items
相比,它假定资源不同,并且没有执行或访问该资源
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.