• node环境下多种方式“get数据解析”


    1、自己写

    const http = require('http');
     
    http.createServer(function(req,res){
        var get = {};
        if(req.url.indexOf('?')!=-1){
            var arr = req.url.split('?');
            var url = arr[0];
            var arr2 = arr[1].split('&');
            for(var i=0;i<arr2.length;i++){
                var arr3 = arr2[i].split('=');
                get[arr3[0]]=arr3[1];
            }
        }else{
            var url = req.url;
        }
        console.log(url,get);
        res.write('aaa');
        res.end();
    }).listen(8080);

    2、引入querystring模块

    const http = require('http');
    const querystring = require('querystring');
    http.createServer(function(req,res){
        var get = {};
        if(req.url.indexOf('?')!=-1){
            var arr = req.url.split('?');
            var url = arr[0];
            get = querystring.parse(arr[1]);
        }else{
            var url = req.url;
        }
        console.log(url,get);
        res.write('aaa');
        res.end();
    }).listen(8080);

    3、引入url模块

    const http = require('http');
    const urlli = require('url');
     
    http.createServer(function(req,res){
        var obj = urlli.parse(req.url,true); //true解析,如果没有true,默认不解析
        var url = obj.pathname;
        var get = obj.query;
        console.log(url,get);
        res.write('aaa');
        res.end();
    }).listen(8080);

    提交表单的HTML代码

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Document</title>
    </head>
    <body>
        <form action="http://localhost:8080/aaa" method="get">
            用户:<input type="text" name="user" value=''><br>
            密码:<input type="password" name="pass" value=''><br>
            <input type="submit" value="提交">
        </form>
    </body>
    </html>

    个人练习如下:

    const http = require("http");
    const queryString = require("querystring");
    const urlNode = require('url');
    
    http.createServer((req,res)=>{
        console.log(req.url);   // /aaa?user=123&pass=456
        var get = {};
        var url = '';
        if(req.url.indexOf('?') != -1){
    
            //方法一:切割字符串
            // var _arr  = req.url.split('?'); //['aaa','user=123&pass=456']
            // url = _arr[0];  //'aaa'
            // var _arr2 = _arr[1].split('&'); //['user=123','pass=456']
            // _arr2.map((v,k,arr)=>{
            //     var key = v.split('=')[0];
            //     var value = v.split('=')[1];
            //     get[key] = value;
            // })
    
            //方法二:正则表达式
            // var query = req.url.substr(req.url.indexOf('?')+1);
            // var regAll = new RegExp("([^?&=]+)=([^?&=]*)", "gi");
            // var regSingle = new RegExp("([^?&=]+)=([^?&=]*)", "i");
            // var arrAll = query.match(regAll);     //["user=123", "pass=456"]
            // arrAll.map(function(v,k,arr){
            //     var resultArr = v.match(regSingle);
            //     get[resultArr[1]] = resultArr[2];
            // })
            //方法三:引入querystring模块
            // const arr =  req.url.split('?');
            // url = arr[0];
            // get = queryString.parse(arr[1]);
            //方法四:
            // const obj = urlNode.parse(req.url,true);
            // url = obj.pathname;
            // get = obj.query;
    
        }else{
            url = req.url;
        }
        console.log(url,get);
        res.write('123456')
        res.end('12')
    }).listen(8000)

     以上都 是原生node实现:

    下面的是express中  req.query 就可以方便的取到 { id 123456}

    app.get('/article',(req,res)=>{
        console.log(req.query)
        
    })

    原文链接:https://blog.csdn.net/alberqing_/article/details/81452487

  • 相关阅读:
    2019-07-15_nginx 启动报错 “/var/run/nginx/nginx.pid" failed” 解决方法
    2019-07-12 linux下关闭、开启防火墙
    2019-07-12nginx的安装,启动,关闭
    2019-07-11 nginx 下网页显示乱码
    2019-07-05 submit左对齐快捷键
    2019-07-02 windows下用cmd命令netstat查看系统端口使用情况
    设计一个精致按钮
    JS 实现省市三级联动
    使用style 对象实现图片的覆盖(overlay)
    display 显示或隐藏元素
  • 原文地址:https://www.cnblogs.com/lguow/p/11790535.html
Copyright © 2020-2023  润新知