NodeJS之http数据解析

来源:互联网 发布:java垃圾回收优点 编辑:程序博客网 时间:2024/06/06 17:19

数据请求

前台: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);