Get请求
query参数的获取
url假设:http://localhost:3000/users/zqzjs?name=zhaoqize&word=cool&job[st]=web
路由的定义:
router.get(‘/:name‘, function(req, res, next) { })
1.获取?name=zhaoqize这样的参数
req.query.name
2.获取&job[st]=web这样的参数
req.query.job.st
3.获取?name=zhaoqize参数的另外一种写法(4.x版本中该方法已被废弃)
req.param(‘name‘)
4.获取所有参数
JSON.stringify(req.query)
具体代码:
//http://localhost:3000/users/zqzjs?name=zhaoqize&word=cool&job[st]=web
//http://localhost:3000/users/zqzjs
router.get(‘/:name‘, function(req, res, next) {
//4.版本中已被废弃,不建议使用
req.param(‘name‘);
console.log(JSON.stringify(req.query) || ‘‘);
}
Post请求
post的数据获取
Form Data pName:zqz pWord:job
1.获取需要的query参数
req.body.pName
具体代码:
router.post(‘/postTest‘,function(req, res, next){ var _html = "[Post]" + "<p><strong>req.body:</strong>"+(JSON.stringify(req.body) || ‘‘)+"</p>" + "<p><strong>req.body.name:</strong>"+(req.body.pName || ‘‘)+"</p>" + "<p><strong>req.body.name:</strong>"+(req.body.pWord || ‘‘)+"</p>"; res.send(_html); })
结果:
[Post] req.body:{"pName":"zqz","pWord":"job"} req.body.name:zqz req.body.name:job