express 请求参数的一些问题

先说点别的,项目入口是index.js,运行 node index 启动项目。

路由部分
app.get('/', function(req, res) { res.send('hello, express'); }); app.get('/users/:name', function(req, res) { res.send('hello, ' + req.params.name); });

在express框架中,第二个路由 :name 是占位符,通过 req.params.name来获取

请求参数主要有几种方式
  • req.query: 解析后的 url 中的 querystring,如 ?name=haha,req.query 的值为 {name: 'haha'}
  • req.params: 解析 url 中的占位符,如 /:name,访问 /haha,req.params 的值为 {name: 'haha'}
  • req.body: 解析后请求体,需使用相关的模块,如 body-parser,请求体为 {"name": "haha"},则 req.body 为 {name: 'haha'}
 
原文地址:https://www.cnblogs.com/yoxiniao/p/7049099.html