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

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,377 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.

Ah, yeah it made total sense to me what's happening now. Thank you!