When to use app.get(“/”,fun(res,req)) and app.post(“/”,fun(res,req)) in node.js?

I am newbie in backend. I need your help. Actually I am confused between app.get() and app.post(). Also when to use them? So pls give me answer in a simple way with examples.

Thank U in advance…!!!!

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

When using the HTTP protocol, we use different URLs to retrieve, add, update or delete information.
To specify which action we want to perform we add to the URL the HTTP method (also called as HTTP verb).

2 popular HTTP verbs are:

  • GET – indicate we want to retrieve / get information (e.g. get information about a user).
  • POST – indicate we want to post / add new information (e.g. add a new user).

In ExpressJS framework we register a handler function (known as a middleware) for each possible route that our app should handle.

to register a handler function to a route that uses the GET method, we use:

.get(<route>, <middleware>)

and to register a handler function to a route that uses the POST method, we use:

.post(<route>, <middleware>)

Method 2


Hi, in API we use


  1. GET to get data
  2. POST to add data
  3. PUT to edit data
  4. PATCH to edit data
  5. DELETE to delete data


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x