req.query and req.param in ExpressJS

后端 未结 4 2243
花落未央
花落未央 2020-11-28 04:01

Main differences between req.query and req.param in Express

  • How are Both different from each other
  • When to use then in wha
4条回答
  •  醉酒成梦
    2020-11-28 04:32

    Passing params

    GET request to "/cars/honda" 
    

    returns a list of Honda car models

    Passing query

    GET request to "/car/honda?color=blue"
    

    returns a list of Honda car models, but filtered so only models with an stock color of blue are returned.

    It doesn't make sense to add those filters into the URL parameters (/car/honda/color/blue) because according to REST, that would imply that we want to get a bunch of information about the color "blue". Since what we really want is a filtered list of Honda models, we use query strings to filter down the results that get returned.

    Notice that the query strings are really just { key: value } pairs in a slightly different format: ?key1=value1&key2=value2&key3=value3.

提交回复
热议问题