您的位置:首页 > 理论基础 > 计算机网络

NodeJS之http数据解析 - get

2017-05-18 17:09 435 查看

数据请求

前台: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);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  nodejs http协议