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);
相关文章推荐
- HttpURLConnection请求数据(GET)并用JSON解析数据(二)
- HttpClient发送get post请求和数据解析
- httpResponse的getEntity为空,获取了这个数据在解析的时候出现了String不能转换成json对象
- HttpClient请求数据(GET)并用JSON解析数据(一)
- HTTP请求协议 get post方法 获取数据解析
- nodejs-http 对form表单上传文件数据的解析过程
- HttpClient发送get post请求和数据解析
- libcurl进行HTTP GET获取解析JSON数据
- socket模拟http中post或者get提交数据,并获得响应数据
- 20110215 学习记录2:表单中的get和post区别 & 使用 HttpWebRequest 向网站提交数据
- php中用socket模拟http中post或者get提交数据
- ASIHTTPRequest iphone下post和get数据的经典类库 配置
- HTTp get,post数据长度的限制
- HttpNameValueCollection处理GET POST提交数据类
- 解析Jquery函数:$.get(),$.post(),$.ajax(),$.getJSON() 操作JSON数据
- ASIHttpRequest+SBJson完成Http请求,以及json数据解析
- HttpWebRequest 向网站提交数据 (post与get方式)
- Android----用HTTP的get,post,HttpClient三种方式向web service提交文本数据
- 程序使用 HTTP 协议和服务器交互主要是进行数据的提交,通常数据的提交是通过 GET 和 POST 两种方式来完成
- [Android开发]Android之使用HTTP的get,post,HttpClient三种方式向服务器端提交文本数据