ExpressJS 中的 req.query 和 req.param [英] req.query and req.param in ExpressJS

查看:27
本文介绍了ExpressJS 中的 req.query 和 req.param的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  • 两者有何不同
  • 什么时候用,什么时候用

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

假设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

看::我引用的程序的这个问题::用于查询结果的简单 Express 程序

推荐答案

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

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

/user?name=tom&age=55 - req.query 将产生 {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<在您的函数中获取表单主体/代码>.这将是 POSTed 表单的解析 JS 版本.

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.

这篇关于ExpressJS 中的 req.query 和 req.param的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆