• 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

  • 相关阅读:
    oo第二次总结
    oo第一次总结
    OO最后一次博客儿
    OO作业总结第三弹
    OO作业总结第二弹
    初学面向对象
    hi🎈
    散列函数及其应用
    结对项目作业
    构建之法第四,第十四章读书有感 (另补第十七章)
  • 原文地址:https://www.cnblogs.com/simadi/p/7017966.html
Copyright © 2020-2023  润新知