7

I'm new on Node.js and Express and now I'm observing that when I change the method on my calling app the params are in req.param([name]), req.body.[name] or req.query and it depends by the method. Now my questions are two:

  1. Are there some differences between these three objects? (something that could explain why a different method refill a different object)
  2. Is there some "problems" if I create a function/module that simple check which is full and, for example, modify the req.body object so I can call this object every time to retrieve the parameters?

EDIT: After @jfriend00 's answer I would explain better my dilemma: I'm developing an api and I would create a module that could check data passed with the different methods, for now I writing something like:

if(req.method== 'PUT' || req.method=='POST')
    x=req.body.x;
else
    x=req.query.x;

and I would do something at the beginning like:

if(req.query!=null)
    req.body=req.query;

so, after, in all my checks I will control over req.body and not the others! Do you think that it is a bad practice?

1 Answer 1

19

All three properties are populated from different sources:

req.query comes from query parameters in the URL such as http://foo.com/somePath?name=ted where req.query.name === "ted".

req.params comes from path segments of the URL that match a parameter in the route definition such a /song/:songid. So, with a route using that designation and a URL such as /song/48586, then req.params.songid === "48586".

req.body properties come from a form post where the form data (which is submitted in the body contents) has been parsed into properties of the body tag.

You use the appropriate property that matches the source of the data you are interested in.

Why do req.params, req.query and req.body exist?

To give you simplified access to three different types of data.

7
  • I edited my question, do you think that I should use all three objects or I can popolate "brutally" one and use all times that? Commented Sep 14, 2016 at 17:02
  • 1
    @Filippo1980 - What you're showing in your edit doesn't make sense to me. You should not be running the same code no matter what the req.method is or no matter how the data was passed in. For example, a method that processes a POST should NOT also be running the same code for a GET. Those are DIFFERENT operations. So, I have no idea why you're trying to write code that doesn't care what the HTTP method was or how the data was passed in. I've never seen any legit reason for that.
    – jfriend00
    Commented Sep 14, 2016 at 17:19
  • Well, I could do some checks to the same parameters ... for example a field date in a post could be use to insert a new row in my database and the same parameter could be use for filter a query during a GET, in this case I would create a sort of module that check all fields and then retrieve an error if someone was wrong ... Commented Sep 15, 2016 at 7:28
  • @Filippo1980 - Yeah, but you wouldn't usually assume a parameter you want to check always comes in the same attribute name every time, so usually you would fetch the value into a vairable from wherever it is expected in this particular request and then call a shared function to validate it.
    – jfriend00
    Commented Sep 15, 2016 at 13:27
  • so, if I understand correctly, I should write, for example, many function to check a single field (to validate the type or the nullability) and not a single function that check a group of fields. Right? Commented Sep 15, 2016 at 15:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.