Why does req.params return an empty array?

后端 未结 3 1055
野的像风
野的像风 2021-02-01 06:10

I\'m using Node.js and I want to see all of the parameters that have been posted to my script. To get to my function, in my routes/index.js I\'m doing:



        
相关标签:
3条回答
  • 2021-02-01 06:42

    req.params only contain the route params, not query string params (from GET) and not body params (from POST). The param() function however checks all three, see:

    http://expressjs.com/4x/api.html#req.params

    0 讨论(0)
  • 2021-02-01 06:53

    I had a similar problem and thought I'd post the solution to that for those coming here for the same reason. My req.params was coming out as an empty object because I declared the URL variable in the parent route. The solution is to add this option to the router:

    const router = express.Router({ mergeParams: true });
    
    0 讨论(0)
  • 2021-02-01 06:57

    req.params
    can only get the param of request url in this pattern:/user/:name

    req.query
    get query params(name) like /user?name=123 or body params.

    0 讨论(0)
提交回复
热议问题