Using multiple parameters in URL in express

前端 未结 2 1911
[愿得一人]
[愿得一人] 2020-12-01 02:07

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 ret

2条回答
  •  难免孤独
    2020-12-01 02:32

    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}
    

提交回复
热议问题