Get Route Parameter Input from the Client

I was very confused to as why this wasn’t working:

app.get("/:word/echo", (req, res) => {
  var word = req.params.word;  
  res.json({echo: word});

I looked at the hint for the problem and figured I had everything formatted correctly. Then I looked at the answer and they have this slight difference from my version:

var { word } = req.params;  

Can someone help me understand why this^^ is different from this:

var word = req.params.word;  

I just want to fully understand before moving on. Thanks

  • you will see difference by yourself, try seeing value of “req.params” in console
  • what value do you get in “console” for this?! it seems alright to me…

Wow they are both the same and they both work. For someone reason I did var word = req.params.word; last night and it wouldn’t work. Now I use it and press submit and its working :man_facepalming:

As said, and as I guess you know by now, the code you posted works just fine. There is no difference in using the property directly or using destructuring, they both work the same(*).

Sometimes we just focus on the wrong thing and miss other errors in the code. But sometimes things just break and stay broken until we restart the environment it is running in. Kind of how a system reboot can fix issues.

(*) At least in this case, there are completely unrelated cases where destructuring isn’t the same, for example, some reactive systems can break when you use destructuring on props (solidjs).

1 Like