103

I am using Express with Node and I have a requirement in which the user can request the URL as: http://myhost/fruit/apple/red.

Such a request will return a JSON response.

The JSON data, before the above call looks like:

{
    "fruit": {
        "apple": "foo"
    }
}  

With the above request, the response JSON data should be:

{
    "apple": "foo",
    "color": "red"
}

I have configured express to route as follows:

app.get('/fruit/:fruitName/:fruitColor', function(request, response) {
    /*return the response JSON data as above using request.params.fruitName and 
request.params.fruitColor to fetch the fruit apple and update its color to red*/
    });  

But this does not work. I am unsure of how to pass multiple parameters, that is, I am unsure if /fruit/:fruitName/:fruitColor is the correct way to do this. Is it?

3 Answers 3

180
app.get('/fruit/:fruitName/:fruitColor', function(req, res) {
    var data = {
        "fruit": {
            "apple": req.params.fruitName,
            "color": req.params.fruitColor
        }
    }; 

    send.json(data);
});

If that doesn't work, try using console.log(req.params) to see what it is giving you.

Sign up to request clarification or add additional context in comments.

5 Comments

Do you know if something like this is possible? /fruit/:fruitName/vegetable/:vegetableName'
sure. just do it like that then do req.params.fruitName and req.params.vegetableName
It works, but it happens that static resources will be addressed under /fruit in this case like /fruit/js/main.js where I have public/js/main.js as my static files folder.
this does not work when one of the parameters is missing
@chovy what is the correct query string to call such an endpoint ?fruitName=${fruitName}&fruitColor=${fruitColor}
43

For what you want I would've used

    app.get('/fruit/:fruitName&:fruitColor', function(request, response) {
       const name = request.params.fruitName 
       const color = request.params.fruitColor 
    });

or better yet

    app.get('/fruit/:fruit', function(request, response) {
       const fruit = request.params.fruit
       console.log(fruit)
    });

where fruit is a object. So in the client app you just call

https://mydomain.dm/fruit/{"name":"My fruit name", "color":"The color of the fruit"}

and as a response you should see:

    //  client side response
    // { name: My fruit name, color:The color of the fruit}

3 Comments

This seems to work just fine and is more extensible if you add params later. I did a JSON.stringify on the client side when building the URL and a JSON.parse on the server in the routing.
Ah, you're correct. I forgot to add JSON's .parse and .stringify to my suggested answer but I do the same too when passing an object as a parameter so I am sure that I am passing the correct form of the object as a string.
Please explain to me how does this work? Giving a stringified JSON as parameter to a GET request. This seems like a really bad idea. If the JSON exceeds the GET char limit then what are you going to do ? Also if the JSON contains some value that breaks the URL encoding it will break (but that's easily fixable though).
7

Both of the way is correct you can use any of them First way

app.get('/fruit/:one/:two', function(req, res) {
    console.log(req.params.one, req.params.two)
});

Another way using & symbol

app.get('/fruit/:one&:two', function(req, res) {
    console.log(req.params.one, req.params.two)
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.