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

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

#1

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


#3

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


#4
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”}