Home > database >  When to use app.get("/",fun(res,req)) and app.post("/",fun(res,req)) in node.js?
When to use app.get("/",fun(res,req)) and app.post("/",fun(res,req)) in node.js?

Time:02-25

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...!!!!

CodePudding user response:


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

CodePudding user response:

Hey I think this question has been already asked earlier please refer the below link for the same: Difference between app.use and app.get in express.js

Thanks

CodePudding user response:

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>)
  • Related