2018-12-06 node.js--第三天

2018-12-06  本文已影响0人  三年_3

\color{red}{querystring.js 解析post和get}

const queryString=require('querystring');
var result=queryString.parse('uname=jack&upwd=123');
console.log(result);
//http://localhost:8080/?uname=jack&upwd=123  获取路径  把数据转换成对象格式{uname:jack,upwd:123}
const http=require('http');
const querystring=require('querystring');
var server=http.createServer(function(req,res){
    var GET={};
    //切割 split('切割符')
    var arr=req.url.split('?');//['/','uname=jack&upwd=123']
    GET=querystring.parse(arr[1]);
    console.log(GET);
})
server.listen(8080);

\color{red}{form表单}

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title></title>
</head>
<body>
    <form action="http://localhost:8080" method="get">
        <p>用户名: <input type="" name="uname"></p>
        <p>密码: <input type="" name="upwd"></p>
        <p><input type="submit" value="提交"></p>
    </form>
</body>
</html>

\color{red}{url模块}

//get 1.手动 2.querystring模块  parse()
//url  模块
var urlLib=require('url');
var result=urlLib.parse('http://www.baidu.com/index?a=3&b=5',true);
//console.log(result);
console.log(result.pathname);
console.log(result.query);

\color{red}{url_form.js}

const http=require('http');
const url=require('url');
var server=http.createServer(function(req,res){
    var obj=url.parse(req.url,true);
    console.log(obj);
});
server.listen(8080);
上一篇下一篇

猜你喜欢

热点阅读