nodejs获取参数的方法

1 获取get的querystring参数

GET /test?name=fred&tel=0926xxx572

let aa = req.param("name");
let bb = req.query.name;

2 post 表单

<form action='/test?id=3' method='post'>
<input type='text' name='name' value='fred'>
<input type='text' name='tel' value='0926xxx572'>
<input type='submit' value='Submit'>
</form>

console.log(req.query.id);

console.log(req.body.name);

console.log(req.body.tel);

3 路径参数

get '/hello/:name/:tel'

console.log(req.params.name);
console.log(req.params.tel);

4 获取特定url参数值
var testUrl = 'http://localhost:8888/select?aa=001&bb=002';
var p = URL.parse(testUrl);
console.log(p.href); //取到的值是:http://localhost:8888/select?aa=001&bb=002
console.log(p.protocol); //取到的值是:http:
console.log( p.hostname);//取到的值是:locahost
console.log(p.host);//取到的值是:localhost:8888
console.log(p.port);//取到的值是:8888
console.log(p.path);//取到的值是:/select?aa=001&bb=002
console.log(p.hash);//取到的值是:null
console.log(p.query);// 取到的值是:aa=001
在此值得注意的是当语句 是 var p = URL.parse(testUrl, true) 时,p.query则返回的是如:{aa:'001'}这样的对象, 直接打印p.query则返回 [object Object],这时我们可以这样 写: console.log(p.query.aa); //取到的值是:001
console.log( p.pathname);//取到的值是:/select

posted @   simadi  阅读(736)  评论(0编辑  收藏  举报
编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)
点击右上角即可分享
微信分享提示