請求數據
前台: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