1、req.body
2、req.query
3、req.params
一、req.body例子
body不是nodejs默认提供的,你需要载入body-parser中间件才可以使用req.body,这个方法通常用来解析POST请求中的数据
<form action='/test' method='post'> <input type='text' name='name' value='lmw'> <input type='text' name='tel' value='1234567'> <input type='submit' value='Submit'> </form> app.post('/test', function(req, res) { console.log(req.body.name); console.log(req.body.tel); });
二、req.query例子
有nodejs默认提供,无需载入中间件,这个方法通常用来解析get请求中的数据
GET /test"color: #ff0000">三、req.query和req.body同时使有<form action='/test"color: #ff0000">四、req.params另一种方法传递参数给服务器,但是这不算是传统标准规范的做法,是属于 HTTP Routing 的延伸应用
GET /test/lmw/123456789 app.get('/test/:name/:tel', function(req, res) { console.log(req.params.name); console.log(req.params.tel); });总结:
req.query: 解析后的 url 中的 querystring,如 "background-color: #ffff99">req.params: 解析 url 中的占位符,如 /:name,访问 /haha,req.params 的值为 {name: 'haha'}
req.body: 解析后请求体,需使用相关的模块,如 body-parser,请求体为 {"name": "haha"},则 req.body 为 {name: 'haha'}
以上这篇node.js获取参数的常用方法(总结)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。
最新资源
- 潘越云.1985-世间女子(滚石25周年经典复刻版)【
- 陈柏宇.2009-CANT.BE.HALF【SONY】【WAV+CUE】
- 李明依.1989-小女生【滚石】【WAV+CUE】
- 《李荣浩 新时代创作新人王》[WAV分轨][310MB]
- 《黑鸭子与马头琴 史上最动听草原音乐》[WAV+CU
- 《汽车发烧音乐精选CD18》[FLAC+CUE][7.1G]
- 郑希怡.2004-Perfect.Date【英皇娱乐】【FLAC分
- 群星.1994-宝丽金超级孖宝2CD【宝丽金】【WAV+C
- 林忆莲.2019-0(2023精装版)【环球】【WAV+CUE】
- Supper.Moment.2016-The.Moment【寰亚】【FLAC分