req.query and req.param in ExpressJS

后端 未结 4 2238
花落未央
花落未央 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:27

    req.query will return a JS object after the query string is parsed.

    /user?name=tom&age=55 - req.query would yield {name:"tom", age: "55"}

    req.params will return parameters in the matched route. If your route is /user/:id and you make a request to /user/5 - req.params would yield {id: "5"}

    req.param is a function that peels parameters out of the request. All of this can be found here.

    UPDATE

    If the verb is a POST and you are using bodyParser, then you should be able to get the form body in you function with req.body. That will be the parsed JS version of the POSTed form.

提交回复
热议问题