请求数据
前台: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('?') != -) {
let arr = url.split('?');
// arr[0] = 地址 '/aaa' arr[1] = 数据 'user=jason&pass=123456'
let arr2 = arr[].split('&');
for(let i = ; i < arr2.length; i++) {
let arr3 = arr2[i].split('=');
// arr3[0] ==>姓名 arr3[1] ==> 密码
GET[arr3[]] = arr3[];
}
}
res.write('有请求了');
res.end();
}).listen();
const http = require('http');

http.createServer( (req, res) => {
console.log(req.url);
let GET = {};
let url = req.url;
if(url.indexOf('?') != -) {
let arr = url.split('?');
url = arr[];
let arr2 = arr[].split('&');
for(let i = ; i < arr2.length; i++) {
let arr3 = arr2[i].split('=');
GET[arr3[]] = arr3[];
}
}
console.log(GET, url)
res.write('');
res.end();
}).listen();
 
提供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: '',
hostname: 'www.baidu.com',
hash: null,
search: '?name=jason&age=18',
query: { name: 'jason', age: '' },
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: '',
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='
}
区别为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();
总结:
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 = ;
req.on('data', (data) => {
console.log(`第${i++}次接收到数据`);
str += data;
});
req.on('end', () => {
let POST = querystring.parse(str);
console.log(str);
});
}).listen();
得到的结果是:
 
第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('');
}else{
res.write('读取成功');
}
res.end();
})
}); server.listen();
【get请求】
/aaa { name: 'jason ', password: '123456', text: 'abcd' } undefined
【post请求】
/aaa {} { name: 'jason ', password: '654321', text: 'zzzz' }
【文件读取】
/1.html {} {}
/favicon.ico {} undefined

最新文章

  1. Python数学函数
  2. 《C#编程》课件 - C#基础
  3. mysql创建表与索引
  4. 【C++】统计代码覆盖率(三)
  5. 【解决】SAE部署Django1.6+MySQL
  6. word中MathType公式不能 二次编辑解决方案
  7. 40. Testing Prev Part IV. Spring Boot features
  8. Python 第五篇(下):系统标准模块(shutil、logging、shelve、configparser、subprocess、xml、yaml、自定义模块)
  9. Converting between IEEE 754 and Float (Format related
  10. httpd日志和日志轮替工具
  11. OI回忆录——一个过气OIer的智障历程
  12. BM算法学习笔记
  13. Vagrant 入门指南
  14. 怎样将virtualbox中的虚拟系统安装到c盘以外的盘
  15. Python面向对象2-类和构造方法
  16. Java Swing 实时刷新JTextArea,以显示不断append的内容?
  17. eclipse server和tomcat的区别,将server的部署目录改到自己安装的tomcat中及如何设置tomcat用户
  18. VS2010工程结构及其瘦身策略
  19. 1-10假期训练(hdu-2059 简单dp)
  20. 【webstorm】project目录树显示不出

热门文章

  1. UTF8和UCS2
  2. .net core执行dotnet ef migrations createmodel等命令出错
  3. 用MVC5+EF6+WebApi 做一个小功能(一)开场挖坑,在线答题系统
  4. Asp.net Core IIS上安装部署及502.5错误解决
  5. 【OCP-12c】CUUG 071题库考试原题及答案解析(21)
  6. 【经典漏洞案例】NSA黑客工具包——Windows 0day验证实验
  7. 【flask】 学习flask macro 模板
  8. flask后端获取前端post/get数据
  9. jquery源码解析:jQuery队列操作queue方法实现的原理
  10. Android 线刷小白教程