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. 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? 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?
Asked
Active
Viewed 937 times
0

Brian Tompsett - 汤莱恩
- 5,753
- 72
- 57
- 129

jordan
- 9,570
- 9
- 43
- 78
-
U can specify the method to be used like this: router.get('/object/delete/:id', myDeleteMethod); router.get('/object/edit/:id', myEditMethod); – ashfaq.p Nov 09 '14 at 06:22
-
@ashfaq.p I know I can do that. I'm asking if I _have_ to. Or if there is a way to specify the http request from the client. – jordan Nov 09 '14 at 06:33
1 Answers
1
In your HTML form
element you can use the method
attribute to specify the method. <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. This can be done with the XmlHttpRequest standard API, jQuery's $.ajax
, or the front end framework of your choosing.
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. 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).

Peter Lyons
- 142,938
- 30
- 279
- 274