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

Tell us what’s happening:

I’m just looking for some clarification really as I’ve done the challenge.

I’m a bit confused about the part that says “The first and last name parameters should be encoded in a query string e.g. ?first=firstname&last=lastname.”

Where does this happen? The code in the challenge just takes the properties out of the req.query object. Does express turn this into a query string automatically or how does this happen? I don’t get get how it is transformed to the format that is asked for

Thanks :slight_smile:

###Your project link(s)

solution: http://localhost:3000

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36

Challenge Information:

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

Hey, first off localhost will only work on your computer youll have to push it to the web with a regular URL which is a whole other issue. Im not sure of the question as it relates to the code but ? is saying to not parse because code=code=something. The names you use here are going to become values not names anymore. Good luck

If you want to know the actual implementation details of the query parsing, will have to look at the source code. The default query parser Express uses is qs.

https://github.com/ljharb/qs/blob/main/lib/parse.js

The server knows the requested URL, so it’s just a matter of parsing it and looking for valid query strings. Using regex or some heuristic for the different parts.


Related


@robheyays You can use localhost for the challenges.

Ok, It just came back with a broken link so I’ll check it again.

You can’t use it, but they can. You can’t connect to their localhost.

1 Like

Oh ok. So its the url that is fed into the function which is then parsed and stored in the res.json object, not the other way round?

The thing that got me was the way the it was worded it said:

“The first and last name parameters should be encoded in a query string e.g. ?first=firstname&last=lastname .”

I thought that meant that it was down to me to make sure that that the parameters were encoded into a query string, not that it was the way the query string would be fed into the function

@robhayays - I didn’t quote the code as I has already completed the task with the hint, and just wanted to know how it was implemented as I didn’t understand where the question mark and all the other symbols that turned it into a url came from. I’ve included it in my answer if you still want to have a look at it :slight_smile:

require('dotenv').config()
let express = require('express');
let app = express();
app.get("/name", function(req,res, next){
    const firstname = req.query.first;
    const lastname = req.query.last;
    res.json({name: `${firstname} ${lastname}`})
})
1 Like