node.js ExpressJS 中的 req.query 和 req.param

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18524125/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 15:27:37  来源:igfitidea点击:

req.query and req.param in ExpressJS

node.jsexpress

提问by arb

Main differences between req.queryand req.paramin Express

Express之间req.queryreq.param中的主要区别

  • How are Both different from each other
  • When to use then in what cases
  • 两者有何不同
  • 什么时候用,什么情况下

Suppose a client sends say Android (Key,value) pair in the request ........ which one to use ?

假设客户端在请求中发送说 Android (Key,value) 对........使用哪个?

[EDIT]

[编辑]

Suppose android sends a POST request -> Intention is to send (Key,Value) to client and the server should perform a database query based on the value in the server and return JSON response

假设android发送一个POST请求->意图是将(Key,Value)发送给客户端,服务器应该根据服务器中的值执行数据库查询并返回JSON响应

Look:: at this question for the program i referenced:: Simple Express program for querying a result

看::这个问题的方案我引用:简单快速的程序,用于查询结果

回答by arb

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

req.query解析查询字符串后将返回一个 JS 对象。

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

/user?name=tom&age=55-req.query会产生{name:"tom", age: "55"}

req.paramswill return parameters in the matched route. If your route is /user/:idand you make a request to /user/5- req.paramswould yield {id: "5"}

req.params将在匹配的路由中返回参数。如果您的路由是/user/:id并且您向/user/5发出请求-req.params将产生{id: "5"}

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

req.param是一个从请求中剥离参数的函数。所有这些都可以在这里找到。

UPDATE

更新

If the verb is a POSTand 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.

如果动词是 aPOST并且您正在使用bodyParser,那么您应该能够在您的函数中使用req.body. 这将是POSTed 形式的解析 JS 版本。

回答by OneOfOne

req.queryis the query string sent to the server, example /page?test=1, req.paramis the parameters passed to the handler.

req.query是发送到服务器的查询字符串,例如/page?test=1req.param是传递给处理程序的参数。

app.get('/user/:id', handler);, going to /user/blah, req.param.idwould return blah;

app.get('/user/:id', handler);,去/user/blahreq.param.id会回来blah

回答by Abhijit Gaikwad

I would suggest using following

我建议使用以下

req.param('<param_name>')

req.param("") works as following

req.param("") 的工作原理如下

Lookup is performed in the following order:

查找按以下顺序执行:

req.params
req.body
req.query

Direct access to req.body, req.params, and req.query should be favoured for clarity - unless you truly accept input from each object.

为了清楚起见,应该支持直接访问 req.body、req.params 和 req.query - 除非您真正接受来自每个对象的输入。

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

参考:http: //expressjs.com/4x/api.html#req.param

回答by Shivam Chhetri

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.

将这些过滤器添加到 URL 参数 (/car/honda/color/blue) 中是没有意义的,因为根据 REST,这意味着我们想要获取关于颜色“蓝色”的一堆信息。由于我们真正想要的是经过过滤的 Honda 车型列表,因此我们使用查询字符串来过滤返回的结果。

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

请注意,查询字符串实际上只是格式稍有不同的 { key: value } 对:?key1=value1&key2=value2&key3=value3。