简体   繁体   中英

NodeJS Express - Difference between GET/POST/PATCH/DELETE behind the scenes

Is .get() , .post() , .put() , .delete() purely semantic with nodeJS and express? (behind the scenes are these methods any different?)

If you're designing an API, at the end of the day you're just sending a req and awaiting a res payload

Can you for instance delete things off a database using a .get() request if you really wanted to?

Or accept a req.body using a .get() request?

What about frontend with axios?

[answer comes from a few senior devs, Andrew Studnicky and Gavin Ray]

Yes you can delete items off a database using a get() request

HTTP verbs are GET/POST/PUT/PATCH/DELETE/OPTIONS. You design your API to match the semantics of the request but there is nothing FORCING you to

I would note that Axios (frontend) in particular has some rules around what can go into request types, especially when bending the rules of REST. For example, a DELETE can technically have a request body (the REST whitepapers do not specify that it cannot) - and while it is not advised, some vendors (such as auth0) do require a requestBody on a DELETE

You should note that axios will strip this out before issuing the request, unless you perform some workaround logic

While you can theoretically do whatever you want with the data once your server gets it, your consumers and coworkers will appreciate if you stick to convention.

Community guidelines exist as a collective contribution of wisdom in design as much as they do in semantics.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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