Queries Vs Params

Good morning.

I’ve been reading up on this topic, but I’m still not sure if I’m understanding this correctly.

Does the following code permit me to console {'name': eric}?

// req.query -> after ?
// req.params -> Can be written within url
app.get('/api/:date?name=bob', (req,res) => {
  console.log(req.query) // {name: bob}
}) 

I am aware I could easily test this on my own console, but it is not working for some reason.

Thank you for replying.

As I understand :date means I would use req.params.date to access the value at date. What I’m caught up on is I’m going to have to immediately use req.query because of the question mark (?). So, if I want to access ‘bob’, would I have to write this?:

req.param.date.query

But this looks very wrong.

/api/:date?name=bob

Let me break this up to explain my logic.

In order to access:
:date: I will need to write req.param.date

name=bob: I will need to write req.query.name

However, I cannot just write req.param.date.req.query.name

I’m confused on how to access bob, when I’m going to need to use both parameters AND queries. How can I even do this?

Regarding your previous comment:

I understand this, however if api was a parameter (so :api), how would I go about it?

One route and one query string, I believe. In the example I used above. I want to access ‘bob’.

Essentially my question boils down to this. I have this path '/api/:date?name=bob', what code do I write to access bob.

I’m sorry if I’m not making sense, this is pretty confusing to me and I appreciate your patience.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.