Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript Express Basics Parameters, Query Strings, and Modularizing Routes Card Template

Ceil-Ian Maralit
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Ceil-Ian Maralit
Front End Web Development Techdegree Graduate 19,434 Points

Where did the req.query came from?

Can someone please explain to me where did all of those came from? Cause I can't seem to get it. Are those terms just made by him?

Also, how did he just use [id][side] without selecting if what he wants is a question or an answer from the json file? I mean, he did not even travel through the object like for example, cards.question or cards.answer, something like that. Can someone please help?

Thank you in advance!

1 Answer

Marco Cornejo
Marco Cornejo
3,411 Points

req is an object, that has a query property. Basically, IF AND ONLY IF your request has a query, it is stored in it (the query property), otherwise NOTHING is stored. And, since the JSON file has the "question" and "answer" as properties of "card", we may perform a query search.

Basically, what the instructor is doing is that, IF AND ONLY IF his URL is: http://localhost:3000/cards/1?side=answer

His code will know that req.query has a Key:Value of side:answer. Answer will be stored in const "side", and the required card and side will be displayed accordingly.

He isn't selecting a question or an answer because he doesn't need to, the user (or a button, or a search) will write that url with the query(key,value) such as '?side=question', but for now he is manually typing the entire URL, which you can watch him doing so in the last few seconds of the video.

I hope I was clear enough.