Request.query和Request.param在ExpressJS中 [英] Request.query and Request.param in ExpressJS
问题描述
- 两者是如何不同的
- 什么时候使用什么情况
假设客户端在请求中发送Android(Key,Value)对,...哪一个要使用? / strong>
Suppose a client sends say Android (Key,value) pair in the request ........ which one to use ?
假设android发送POST请求 - >意图是将(Key,Value)发送给客户端,服务器应根据服务器中的值执行数据库查询并返回JSON响应
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
查看::我参考的程序的这个问题::用于查询结果的简单快捷程序
推荐答案
req.query
将在查询字符串解析后返回一个JS对象。
req.query
will return a JS object after the query string is parsed.
/ user?name = tom& age = 55 - req.que ry
将产生 {name:tom,age:55}
req.params
将返回匹配路由中的参数。
如果你的路由是 / user /:id ,你向 / user / 5请求 - req.params
将产生 {id:5}
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
是一个从请求中剥离参数的功能。所有这些都可以在这里找到。
req.param
is a function that peels parameters out of the request. All of this can be found here.
更新
如果动词是 POST
,而您正在使用 bodyParser
,那么您应该可以使用 req.body
获取表单体。这将是解析的JS版本的 POST
ed表单。
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 POST
ed form.
这篇关于Request.query和Request.param在ExpressJS中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!