Basic Node and Express - Get Route Parameter Input from the Client

Hi while trying to solve this i see that it can be done by two ways… please could someone tell me if there is a particular way to follow or are both acceptable methods as long as its passing the tests.
thank you.

1st method:

app.get(’/:echo/echo’, function(req, res) {
res.json(req.params);
});

2nd method:

app.get(’/:word/echo’, function(req, res) {
res.json({‘echo’: req.params.word});
});

PS. please let me know if there is a correct way to go around or if it doesnt matter at this point

Though only 2nd Method is passing the test but as both are giving same solution so both of them are correct. 1st Method is more explicit and easy to read and work when multiple parameters are there

1 Like
app.get('/:word/echo', function(req, res){
console.log(req.params.word)
})

If the above is used, when you submit the url in the lesson, the console prints out:
ech0-t3st
eChOtEsT

Even though it doesn’t pass that lesson, it shows what is returned.

/*************************************************************/
When I enter this URL https://jagged-cold.glitch.me/:word/echo in the browser, assuming that I typed the code correctly, the browser page should be
{“echo”: “word”}

Can anyone tell me what I am doing wrong here?
https://axiomatic-athlete.glitch.me/word/echo

image

I get the:
// running tests
Not Found
Not Found
// tests completed

Sorry for the late reply. Getting back to the swing of things. Were you able to get it solved? Looks as though the let word = req.params.word continues the dot notation into the res.json({"echo":word}).

Also what version of express do you have in package.json?

app.get(’/:word/echo’, function(req, res){
res.json({echo: req.params.word})
console.log(req.params.word)
})