美文网首页
nodeJs接收接口传来的参数

nodeJs接收接口传来的参数

作者: 绿啊绿啊绿刺猬 | 来源:发表于2020-06-20 23:26 被阅读0次

    参考文章:https://www.cnblogs.com/ostrich-sunshine/p/6741180.html
    https://www.cnblogs.com/ywjfx/p/10403912.html

    --------------------------------下面是自己的记录---------------------------------------
    一开始是跟着视频学习的,老师用的接收方法是req.param(),但是打印之后一直是undefined,后面查到原来这个方法已经是弃用了,get方法用req.query,post方法用req.body。
    req.query方法可以直接用,但是req.body方法要安装中间件body-parser才可以:

    npm install body-parser --save
    

    然后引用:

    import bodyParser= require("body-parser")
    app.use(bodyParser.urlencoded({extended:false}));
    app.use(bodyParser.json());
    
    image.png

    这样子就可以啦~
    在用的时候:

    let username = req.query.username;   // get
    let password = req.body.password;    // post
    

    相关文章

      网友评论

          本文标题:nodeJs接收接口传来的参数

          本文链接:https://www.haomeiwen.com/subject/lshnxktx.html