NodeJS之http数据解析 - get

数据请求

前台:form , ajax , jsonp
后台:都一样
无论前后台如何,都是通过http协议 , 前台 <-> 后台

请求方式

GET,数据在url
POST,数据不在url
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title></title>
    <style type="text/css"></style>
</head>
<body>
    <form action="http://localhost:8080/mango" method="get">
        uName: <input type="text" name="user" value=""> <br>
        passwd: <input type="password" name="psd" value=""> <br>
        <input type="submit" value="submit">
    </form>
</body>
</html>
const http = require('http');
var server = http.createServer(function(req, res) {
    //req:获取前台过来的请求
    console.log(req.url);// req.url = '/mango?user=guoyu&password=123456'
    //   /mango?user=guoyu&psd=123456
    var GET = {};

    if (req.url.indexOf('?') != -1) {//chrome经常请求favicon.ico而且不带?所以要把这个废物排出
        var arr = req.url.split('?');
        var url = arr[0];//地址   '/mango'
        var arr2 = arr[1].split('&');

        for (var i = 0; i < arr2.length; i++) {
            var arr3 = arr2[i].split('=');
            //arr3[0] => 名字  'user'
            //arr3[1] => 数据   'guoyu'
            GET[arr3[0]] = arr3[1];
        }
    } else {
        var url = req.url;
    }


    console.log(url, GET);
//   /mango { user: 'guoyu', psd: '123456' }
//   /favicon.ico
//   /favicon.ico {}



    res.write('abc123');
    res.end();
});

server.listen(8080);

上面的代码过于繁杂,可以直接引用一个模块,简化代码
const http = require('http');
var querystring = require('querystring');


// var json = querystring.parse('user=blue&pass=123456&age=18');//直接返回一个json
// console.log(json);
// { user: 'blue', pass: '123456', age: '18' }

var server = http.createServer(function(req, res) {
    //req:获取前台过来的请求
    console.log(req.url);// req.url = '/mango?user=guoyu&password=123456'

    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);
//   /mango { user: 'guoyu', psd: '123456' }
//   /favicon.ico
//   /favicon.ico {}



    res.write('abc123');
    res.end();
});

server.listen(8080);
其实还有更简单的
const urlLib = require('url');
const urlLib = require('url');

//true:是否解析query属性
var obj = urlLib.parse('http://www.baidu.com/index.html?uname=guoyu&age=27&sex=male',true);
console.log(obj);


Url {
  protocol: 'http:',
  slashes: true,
  auth: null,
  host: 'www.baidu.com',
  port: null,
  hostname: 'www.baidu.com',
  hash: null,
  search: '?uname=guoyu&age=27&sex=male',
  query: { uname: 'guoyu', age: '27', sex: 'male' },
  pathname: '/index.html',
  path: '/index.html?uname=guoyu&age=27&sex=male',
  href: 'http://www.baidu.com/index.html?uname=guoyu&age=27&sex=male'
}



var obj1 = urlLib.parse('http://www.baidu.com/index.html?uname=guoyu&age=27&sex=male');
console.log(obj1);


Url {
  protocol: 'http:',
  slashes: true,
  auth: null,
  host: 'www.baidu.com',
  port: null,
  hostname: 'www.baidu.com',
  hash: null,
  search: '?uname=guoyu&age=27&sex=male',
  query: 'uname=guoyu&age=27&sex=male',
  pathname: '/index.html',
  path: '/index.html?uname=guoyu&age=27&sex=male',
  href: 'http://www.baidu.com/index.html?uname=guoyu&age=27&sex=male' }

const http = require('http');
const querystring = require('querystring');
const urlLib = require('url');

var server = http.createServer(function(req, res) {
    console.log(req.url);

    var obj = urlLib.parse(req.url, true);
    var url = obj.pathname;
    var GET = obj.query;

    console.log(url, GET);
//   /mango { user: 'guoyu', psd: '123456' }
//   /favicon.ico
//   /favicon.ico {}



    res.write('abc123');
    res.end();
});

server.listen(8080);
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值