一、url網址解析
url.parse(urlString[, parseQueryString[, slashesDenoteHost]])
url.format(urlObject)
url.resolve(from, to)
┌─────────────────────────────────────────────────────────────────────────────┐
│ href │
├──────────┬┬───────────┬─────────────────┬───────────────────────────┬───────┤
│ protocol ││ auth │ host │ path │ hash │
│ ││ ├──────────┬──────┼──────────┬────────────────┤ │
│ ││ │ hostname │ port │ pathname │ search │ │
│ ││ │ │ │ ├─┬──────────────┤ │
│ ││ │ │ │ │ │ query │ │
" http: // user:pass @ host.com : 8080 /p/a/t/h ? query=string #hash "
│ ││ │ │ │ │ │ │ │
└──────────┴┴────���──────┴──────────┴──────┴──────────┴─┴──────────────┴───────┘
(all spaces in the "" line should be ignored -- they are purely for formatting)
https://www.baidu.com:8080/api.php?from=1000phone&course=nodejs#testphp
1 { 2 protocol: 'https:', 3 slashes: true, 4 auth: null, 5 host: 'www.baidu.com:8080', 6 port: '8080', 7 hostname: 'www.baidu.com', 8 hash: '#test', 9 search: '?from=1000phone&course=nodejs', 10 query: { from: '1000phone', course: 'nodejs' }, 11 pathname: '/api.php', 12 path: '/api.php?from=1000phone&course=nodejs', 13 href: 'https://www.baidu.com:8080/api.php?from=1000phone&course=nodejs#test' }
執行命令
node
url
url.parse("https://www.baidu.com")
url.parse("https://www.baidu.com:8080/api.php?from=qianfeng&course=node#level")-----query爲字符串
url.parse("https://www.baidu.com:8080/api.php?from=qianfeng&course=node#level",true)-----query爲對象
url.parse("https://www.baidu.com",true)
url.parse("//www.baidu.com:8080/api.php?from=qianfeng&course=node#level",true)--沒有解析到協議和端口號
url.parse("//www.baidu.com:8080/api.php?from=qianfeng&course=node#level",true)--解析到host和port
將以前的解析出來的使用url.format()執行,能夠解析出一個url地址
兩段url解析成一個url.resolve("http://www.baidu.com","/api/list.php");
二、querystring網址:http://nodejs.cn/api/html
1 querystring.escape(str) 2 querystring.parse(str[, sep[, eq[, options]]]) 3 querystring.stringify(obj[, sep[, eq[, options]]]) 4 querystring.unescape(str) 5 6 node 7 querystring 8 querystring.stringify({name:"qianfeng",course:["nodejs","vue.js"],from:""}) 9 querystring.stringify({name:"qianfeng",course:["nodejs","vue.js"],from:""},",")----設置分隔符 10 querystring.stringify({name:"qianfeng",course:["nodejs","vue.js"],from:""},",",":") 11 querystring.parse(默認的字符串) 12 13 querystring.escape({"from":"beijing"}}); 14 querystring.escape("北京")
三、http模塊
四、http小爬蟲
建立一個文件spider.js
網址:https://www.lagou.comvue
1 var http = require("http"); 2 var https = require("https"); 3 var url = "https://www.lagou.com"; 4 https.get(url,function(res){ 5 var html=""; 6 res.on("data",function(data){ 7 html+=data; 8 ) 9 res.on("end",function(){ 10 console.log(html); 11 }) 12 res.on("error",function(err){ 13 console.log(err); 14 }) 15 })
引入一個第三方工具
cnpm install cheerio --save-dev
注意:
--save表示安裝到本地---項目依賴--dependencies
-dev表示安裝到開發依賴----devDependencies---項目上線時不須要開發依賴
node
1 var http = require("http"); 2 var https = require("https"); 3 var cheerio = require("cheerio"); 4 var url = "https://www.lagou.com"; 5 function filter(html){ 6 var $ = cheerio.load(html); 7 var menu = $(".menu_main"); 8 var menuData = []; 9 menu.each(function(index,value){ 10 var menuTitle = $(value).find("h2").text(); 11 var menuLists = $(value).find("a"); 12 var menulist = []; 13 menuLists.each(function(index,value){ 14 menulist.push($(value).text()) 15 }) 16 menuData.push({menuTitle:menuTitle,menulist:menulist}) 17 }) 18 return menuData 19 20 21 } 22 23 https.get(url,function(res){ 24 var html=""; 25 res.on("data",function(data){ 26 html+=data; 27 ) 28 res.on("end",function(){ 29 console.log(html); 30 var result = filter(html); 31 printMenu(result); 32 }) 33 res.on("error",function(err){ 34 console.log(err); 35 }) 36 }) 37 38 function printMenu(menu){ 39 menu.forEach(function(value){ 40 console.log(value.menuTitle+"\n"); 41 value.menuList.forEach(function(value){ 42 console.log(value); 43 } 44 ) 45 }) 46 }
五、request方法
新建文件requestGet.js https://developers.douban.com/wiki/?title=movie_v2#top250
請求的數據http://api.douban.com/v2/movie/top250
http://json.cn/ajax
1 const https = require('https') 2 var options = { 3 hostname: 'api.douban.com', 4 port: 443, 5 method: 'GET', 6 path: '/v2/movie/top250' 7 } 8 var responseData = '' 9 var request = https.request(options, (response) => { 10 // console.log(response) 11 // console.log(response.statusCode) 12 // console.log(response.headers) 13 response.setEncoding('utf8') 14 response.on('data', (chunk) => { 15 responseData += chunk 16 }) 17 response.on('end', () => { 18 JSON.parse(responseData).subjects.map((item) => { 19 console.log(item.title) 20 }) 21 }) 22 }) 23 24 request.on('error', (error) => { 25 console.log(error) 26 }) 27 28 request.end()
新建文件requestPost.jsnpm
1 const http = require('http') 2 const querystring = require('querystring') 3 4 var postData = querystring.stringify({ 5 'question[title]':'這個視頻不錯2', 6 'question[content]':'<p>贊一個</p>', 7 'question[courseId]':'227', 8 'question[lessonId]':'1753', 9 '_csrf_token':'32b0e1e07c657000e1f7250cdeac0377e6af688f' 10 }) 11 12 var options = { 13 hostname: 'www.codingke.com', 14 port: 80, 15 method: 'POST', 16 path: '/ajax/create/course/question', 17 headers: { 18 'Accept':'*/*', 19 'Accept-Encoding':'gzip, deflate', 20 'Accept-Language':'zh-CN,zh;q=0.8,en;q=0.6', 21 'Connection':'keep-alive', 22 'Content-Length': postData.length, 23 'Content-Type':'application/x-www-form-urlencoded; charset=UTF-8', 24 'Cookie':'PHPSESSID=qsqkoo0bq1grc6a5jo7kgk5tn7; CNZZDATA1256018185=1839628736-1476447839-null%7C1485424829; Hm_lvt_9f92046de4640f3c08cf26535ffdd93c=1485410187,1485420976; Hm_lpvt_9f92046de4640f3c08cf26535ffdd93c=1485426828', 25 'Host':'www.codingke.com', 26 'Origin':'http://www.codingke.com', 27 'Referer':'http://www.codingke.com/v/398-chapter-227-course', 28 'User-Agent':'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.95 Safari/537.36', 29 'X-CSRF-Token':'32b0e1e07c657000e1f7250cdeac0377e6af688f', 30 'X-Requested-With':'XMLHttpRequest' 31 } 32 } 33 34 var request = http.request(options, (res) => { 35 console.log('Status:' + res.statusCode) 36 console.log('headers:' + JSON.stringify(res.headers)) 37 res.setEncoding('utf8') 38 res.on('data', (chunk) => { 39 console.log(chunk) 40 }) 41 res.on('end', () => { 42 console.log('技術問答提交完畢!') 43 }) 44 }) 45 46 request.on('error', (error) => { 47 console.log(error) 48 }) 49 50 request.write(postData) 51 52 request.end()
七、event事件
events
使用事件:EventEmitter
事件的參數
只執行一個的事件監聽器
建立一個文件event_emitter.jsjson
1 const EventEmitter = require('events') 2 class Player extends EventEmitter {} 3 var player = new Player() 4 player.once('play', (track) => { 5 console.log(`正在播放:《${track}》`) 6 }) 7 player.emit('play', '精絕古城') 8 player.emit('play', '黃皮子墳')
八、File System
獲得文件與目錄的信息:stat
建立一個目錄:mkdir
建立文件並寫入內容:writeFile,appendFile
讀取文件的內容:readFile
列出目錄的東西:readdir
重命名目錄或文件:rename
刪除目錄與文件:rmdir,unlink
建立fs_1.js---查看狀態api
const fs = require('fs') fs.stat('hello.js', (error, stats) =>{ if(error){ console.log(error) } else { console.log(stats) console.log(`文件:${stats.isFile()}`) console.log(`目錄:${stats.isDirectory()}`) } })
建立fs_2.js---建立目錄app
const fs = require('fs') fs.mkdir('logs', (error) => { if(error){ console.log(error) } else { console.log('成功建立目錄:logs') } })
建立fs_3.js---寫入文件less
1 const fs = require('fs') 2 fs.writeFile('logs/hello.log', '您好 ~ \n', (error) => { 3 if(error) { 4 console.log(error) 5 } else { 6 console.log('成功寫入文件') 7 } 8 }) 9 10 fs.appendFile('logs/hello.log', 'hello ~ \n', (error) => { 11 if(error) { 12 console.log(error) 13 } else { 14 console.log('成功寫入文件') 15 } 16 })
建立fs_4.js---讀取文件
1 const fs = require('fs') 2 fs.readFile('logs/hello.log', 'utf8', (error, data) =>{ 3 if (error) { 4 console.log(error) 5 } else { 6 console.log(data) 7 } 8 })
建立fs_5.js---讀取文件夾
1 const fs = require('fs') 2 fs.readdir('logs', (error, files) => { 3 if (error) { 4 console.log(error) 5 } else { 6 console.log(files) 7 } 8 })
建立fs_6.js--文件重命名
1 const fs = require('fs') 2 fs.rename('logs/hello.log', 'logs/greeting.log', (error) =>{ 3 if (error) { 4 console.log(error) 5 } else { 6 console.log('重命名成功') 7 } 8 })
建立fs_7.js--刪除目錄
1 const fs = require('fs') 2 fs.readdirSync('logs').map((file) => { 3 fs.unlink(`logs/${file}`, (error) => { 4 if (error) { 5 console.log(error) 6 } else { 7 console.log(`成功的刪除了文件: ${file}`) 8 } 9 }) 10 }) 11 12 fs.rmdir('logs', (error) =>{ 13 if (error) { 14 console.log(error) 15 } else { 16 console.log('成功的刪除了目錄:logs') 17 } 18 })
九、Stream
讀取文件流
可讀流的事件
可寫的文件流
pipe
鏈式使用 pipe
建立文件stream.js
1 const fs = require('fs') 2 var fileReadStream = fs.createReadStream('data.json') 3 var fileWriteStream = fs.createWriteStream('data-1.json') 4 var count = 0 5 fileReadStream.once('data', (chunk) => { 6 console.log(chunk.toString()) 7 fileWriteStream.write(chunk) 8 }) 9 fileReadStream.on('data', (chunk) => { 10 console.log(`${ ++count } 接收到:${chunk.length}`) 11 fileWriteStream.write(chunk) 12 }) 13 fileReadStream.on('end', () => { 14 console.log('--- 結束 ---') 15 }) 16 fileReadStream.on('error', (error) => { 17 console.log(error) 18 })
流的操做
1 const fs = require('fs') 2 var fileReadStream = fs.createReadStream('data.json') 3 var fileWriteStream = fs.createWriteStream('data-1.json') 4 fileReadStream 5 .pipe(fileWriteStream)
進一步處理---寫入以前進行壓縮
1 const fs = require('fs') 2 const zlib = require('zlib') 3 4 var fileReadStream = fs.createReadStream('data.json') 5 var fileWriteStream = fs.createWriteStream('data.json.gz') 6 7 fileWriteStream.on('pipe', (source) => { 8 console.log(source) 9 }) 10 11 fileReadStream 12 .pipe(zlib.createGzip()) 13 .pipe(fileWriteStream)