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:
- Are there some differences between these three objects? (something that could explain why a different method refill a different object)
- 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?
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 Sep 14 '16 at 17:19req.query["min-price"]
. – jfriend00 Mar 01 '18 at 16:32