1

I saw an example of app.post() function. What does the '/' mean? Are we required to use post and get methods in conjunction or can we just use one method?

app.post('/', function(req, res){
    return;
});
Pickels
  • 33,902
  • 26
  • 118
  • 178
jensiepoo
  • 571
  • 3
  • 9
  • 26

4 Answers4

2

The '/' is the root directory of your website. So that function would handle post requests for foobar.com/ . You don't have to use post and get methods in conjunction. Normally I use get and only use post for routes that I want to receive post data.

sheldonk
  • 2,604
  • 3
  • 22
  • 32
1

The code you posted means you're setting up the server to "listen" to the root url and execute the callback when the browser hits that url.

So, assuming you're using port 80, your url would be: http://localhost:80/ Since you're using the post method, then the callback will be executed when a post request is received on that url.

If you were to instead, use the get method, then you could just navigate to that url writing it on your browser address bar.

That way you can set all the endpoints for your web app.

Edit

If you want to know when to use post, get, and the other methods, you might want to check out this answer: Understanding REST: Verbs, error codes, and authentication

Community
  • 1
  • 1
Deleteman
  • 8,500
  • 6
  • 25
  • 39
0

when you call app.post or app.get, you are listening for post or get requests, respectively. The first argument to these calls is the route at which you are listening for the request. so in the code below:

app.post('/', function (req,res) {
    res.send("hello");
}

you are telling the server to call that function when someone makes a post request to the root of your domain (mydomain.com/).

likewise, the code below would tell the server to listen for get requests at "/getroute" (mydomain.com/getroute).

app.get('/getroute', function (req, res) {
    res.send('hello');
}

post requests and get requests can be used seperately and do not have to be used in conjunction on the same route.

Jaime Morales
  • 369
  • 1
  • 4
0

Look, the first parameter of app.post() is the route at which post data is received, which is sent by HTML form(action = '/') mean action attribute of your form tag, it is the route at which your HTML form will send your data. So, it no connection with the app.get parameter.