码迷,mamicode.com
首页 > Web开发 > 详细

nodejs学习笔记二(get请求、post请求、 querystring模块,url模块)

时间:2017-06-22 00:08:33      阅读:515      评论:0      收藏:0      [点我收藏+]

标签:总结   length   lob   常用   res   div   简写   大小   require   

请求数据
前台:form、ajax、jsonp
后台:接受请求并返回响应数据
 
 
前台《= http协议 =》后台
 
常用的请求的方式:
1、GET           数据在url中
2、POST         数据不再url中
 
get方式:通过地址栏来传输     name=value&name1=value1&               作用:分享
 
post方式:通过head头传输      数据相对安全
 
form
 
     action="http://www.vaidu.com"          地址
     method:"post/get"                              传输方式
 
 
window.location.pathname         路径  也可以做跳转               www.baidu.com
window.location.port                  端口号                                   8080
window.location.hash                  锚点                                   #后面的
window.location.protocol           协议                                     http/https
window.location.search               数据                                   ?号后面的   name=value&name1=value1
 
浏览器 《=   通信数据   =》 服务器
 
通信数据:
1、头(header)
2、请求主体(content)
 
 
【GET方式】
<form action="http://localhost:8080/" method="get">
     用户:<input type="text" name="user" value="" /><br>
     密码:<imput type="password" name="pass" value="" /><br>
     <input type="submit" value="提交">
</form>
const http = require(http);

http.createServer( (req,res) => {
     console.log(req.url);    //  /aaa?user=jason&pass=123456
     let GET = {};
     let url = req.url;
     // 判断请求的url中是否含有查询参数
     if(url.indexOf(?) != -1) {
     let arr = url.split(?);
     // arr[0] = 地址 ‘/aaa‘     arr[1] = 数据 ‘user=jason&pass=123456‘
     let arr2 = arr[1].split(&);
     for(let i = 0; i < arr2.length; i++) {
          let arr3 = arr2[i].split(=);
          // arr3[0] ==>姓名    arr3[1] ==> 密码
          GET[arr3[0]] = arr3[1];
     }  
}
     res.write(有请求了);
     res.end();
}).listen(8080);
const http = require(http);

http.createServer( (req, res) => {
    console.log(req.url);
    let GET = {};
    let url = req.url;
    if(url.indexOf(?) != -1) {
        let arr = url.split(?);
        url = arr[0];
        let arr2 = arr[1].split(&);
        for(let i = 0; i < arr2.length; i++) {
            let arr3 = arr2[i].split(=);
            GET[arr3[0]] = arr3[1];
        }
    }
    console.log(GET, url)
    res.write(1111111);
    res.end();
}).listen(8799);
 
提供querystring(查询字符) 模块
const querystring = require(querystring);
let GET = querystring.parse(name=jason&age=18);
console.log(GET);

// { name: ‘jason‘, password: ‘123456‘ } ‘/‘
// {} ‘/favicon.ico‘

url模块:

const urlLib = require(url);

let urlObj = urlLib.parse(http://www.baidu.com:8901/index/static?name=jason&age=18, true);

console.log(urlObj);

当第二个参数为true时:

Url {
  protocol: http:,
  slashes: true,
  auth: null,
  host: www.baidu.com:8901,
  port: 8901,
  hostname: www.baidu.com,
  hash: null,
  search: ?name=jason&age=18,
  query: { name: jason, age: 18 },
  pathname: /index/static,
  path: /index/static?name=jason&age=18,
  href: http://www.baidu.com:8901/index/static?name=jason&age=18
}

为false时:

Url {
  protocol: http:,
  slashes: true,
  auth: null,
  host: www.baidu.com:8901,
  port: 8901,
  hostname: www.baidu.com,
  hash: null,
  search: ?name=jason&age=18,
  query: name=jason&age=18,
  pathname: /index/static,
  path: /index/static?name=jason&age=18,
  href: http://www.baidu.com:8901/index/static?    
  name=jason&age=18}
区别为query是否被querystring
 
于是上面的例子可以被简写为
const http = require(http);
const urlLib = require(url);

http.createServer( (req, res) => {
    let urlObj = urlLib.parse(req.url, true);
    let GET = urlObj.query;
    let urlName = urlObj.pathname;
    console.log(GET, urlName);
    res.write(完成);
    res.end();
}).listen(8998);
总结:
1.querystring模块只能解析     query
2.url可以解析整个url
 
【post请求】
请求头最大      32k
请求主体最大   1G
 
数据量的大小产生了数据处理方式的区别
 
处理很大的数据---分段处理  (防止堵塞和出错)
 
1.data事件   有一段数据到达的时候,可以发送很多次
req.on(‘data‘,  回调函数 function(data) {
     
});
2.end事件     数据全部到达的时候,只执行一次
     req.on(‘end‘, () => {
 
     });
<form action="http://localhost:8999/aaa" method="post">
        账号:<input type="text" name="name" value=""><br>
        密码:<input type="password" name="password" value=""><br>
        <input type="submit" value="提交">
</form>
const http = require(http);
const querystring = require(querystring);

http.createServer( (req, res) => {
     // post -- req
     let str = ‘‘;
     let i = 0;
     req.on(data, (data) => {
          console.log(`第${i++}次接收到数据`);
         str += data; 
     });
     req.on(end, () => {
          let POST = querystring.parse(str);
          console.log(str);
     });
}).listen(8999);
得到的结果是:
 
第0次接收到数据
{
  name: ‘jason‘,
  password: ‘123456‘,
}
const http = require(http);
const fs = require(fs);
const querystring = require(querystring);
const urlLib = require(url);

let server = http.createServer( (req, res) => {
    // GET
    let url = urlLib.parse(req.url).pathname;
    const GET = urlLib.parse(req.url, true).query;
    // POST
    let str = ‘‘;
    let POST;
    req.on(data, (data) => {
        str += data;
    });
    req.on(end, () => {
        POST = querystring.parse(str);
        console.log(url, GET, POST);
    });
    console.log(url, GET, POST);
    // 文件读取
    let file_name = ./www + url;
    fs.readFile(file_name, (err, data) => {
        if(err) {
            res.write(404);
        }else{
            res.write(读取成功);
        }
        res.end();
    })
});

server.listen(8999);
【get请求】
/aaa { name: ‘jason ‘, password: ‘123456‘, text: ‘abcd‘ } undefined
【post请求】
/aaa {} { name: ‘jason ‘, password: ‘654321‘, text: ‘zzzz‘ }
【文件读取】
/1.html {} {}
/favicon.ico {} undefined

nodejs学习笔记二(get请求、post请求、 querystring模块,url模块)

标签:总结   length   lob   常用   res   div   简写   大小   require   

原文地址:http://www.cnblogs.com/jasonwang2y60/p/7062053.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!