简体   繁体   English

节点/表达-如何实现DELETE和PUT请求

[英]Node/Express - How to implement DELETE and PUT requests

I know that I can route to router.get('/object/:id', ...) , router.post('/object/new', ...) , router.delete('/object/:id', ...) , and router.put('/object/:id', ...) and that when I browse to a specific object, the browser will issue a http get request. 我知道我可以路由到router.get('/object/:id', ...) router.post('/object/new', ...)router.delete('/object/:id', ...)router.put('/object/:id', ...) ,当我浏览到特定对象时,浏览器将发出http get请求。 And I understand that I can post info through a form. 我知道我可以通过表格发布信息。 But how can I implement the DELETE and PUT methods so that I can edit and delete objects? 但是,如何实现DELETEPUT方法,以便可以编辑和删除对象? How do I specify the method used in the route? 如何指定路线中使用的方法? Do I have to change the route so that it is unique (ie, router.get('/object/delete/:id', ...) and router.get('/object/edit/:id', ...) ) and just use get methods? 我是否需要更改路由以使其唯一(即, router.get('/object/delete/:id', ...)router.get('/object/edit/:id', ...) ),只是使用get方法?

In your HTML form element you can use the method attribute to specify the method. 在HTML form元素中,可以使用method属性指定方法。 <form method="put"> . <form method="put"> However, more typically these type of RESTful API endpoints are called from browsers with javascript as AJAX requests, which can use all of the available HTTP methods. 但是,更典型的是,这些类型的RESTful API终结点是通过具有javascript作为AJAX请求的浏览器来调用的,该浏览器可以使用所有可用的HTTP方法。 This can be done with the XmlHttpRequest standard API, jQuery's $.ajax , or the front end framework of your choosing. 这可以通过XmlHttpRequest标准API,jQuery的$.ajax或您选择的前端框架来完成。

Do I have to change the route so that it is unique 我是否必须更改路线以使其唯一

No, you can have the same URL path with different HTTP methods and those can be handled by different callback functions to behave differently. 不,您可以使用不同的HTTP方法使用相同的URL路径,并且可以由不同的回调函数处理这些URL行为以产生不同的行为。 Conventional REST URL schemes make heavy semantic use of the various HTTP methods requesting the same URL path (GET means get, PUT means replace, etc). 常规的REST URL方案在语义上大量使用了请求同一URL路径的各种HTTP方法(GET表示获取,PUT表示替换等)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM