亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定

進擊Node.js基礎(二)

Scott 全棧工程師
難度中級
時長 2小時 4分
學習人數
綜合評分9.60
153人評價 查看評價
9.8 內容實用
9.4 簡潔易懂
9.6 邏輯清晰
  • 一、buffer: tcp/圖像/文件/網絡

    1、poolSize: 內存載體的容量

    2、isBuffer

    3、compare:判斷buffer對象的相對位置,字符串排序

    4、isEncoding:nodeJs是否支持某種編碼

    5、concat:連接buffer對象

    6、byteLength:獲得指定編碼下字節長度。

    二、buffer源碼,1000行左右

    1、process.binding使javascript代碼和c++代碼能夠進行交互。

    2、smalloc:操作內存和分配內存的對象

    smalloc.alloc:生成內存的方法

    查看全部
  • 一、buffer和stream

    1、buffer

    (1)緩沖,nodeJs處理二進制的數據。js字符串數據‘utf-8’格式。

    (2)全局的,不需要require引入。是一個構造函數,有自己的屬性和方法。

    (3)實例化Buffer

    ①new 一個實例化對象

    new Buffer('hello');

    ② 內存空間實例化

    var buf = new Buffer(8); // 8:內存空間的大小

    ③?數組方式實例化的對象,可以直接用下標訪問。通過下標取到的值是直接取整,如果原先是小數,取出來的就是整數了。

    var buf = new Buffer([1, 2, 3, 4]);?



    查看全部
  • 一、https協議是在http協議的基礎上添加了SSL和TLS

    二、https模塊主要是處理加密訪問的。搭建https的時候需要ssl證書。

    三、并發控制,同時去爬。Promise.all();


    查看全部
  • 一、異步操作解決方案:

    回調

    事件機制

    對事件進行監聽

    對某個異步操作增加事件的觸發,包括訂閱者、發布者等觀察者模式

    promise

    二、Promise是一個對象,與其他javascript對象沒有其他區別

    三、Promise對象的三種狀態

    1、未完成 pending

    2、已完成 fufilled

    3、 失敗 rejected

    四、jquery中的promise,then返回的不是新的promise,只是改變了promise的狀態。

    五、promise then 方法:

    1、then方法必須是返回一個promise對象。

    2、then接收2個參數,這2個參數都可以省略。

    3、promise會保證then的回調順序。



    查看全部
  • 一、webstorm列編輯,多行編輯快捷鍵:

    按住alt鍵鼠標選擇要修改的字符串,然后輸入文字就會編輯多行。

    二、動畫一般一秒鐘60幀,是比較流暢的幀率。

    三、安裝bluebird,aSuncat:現在的nodejs不安裝bluebird就能用promise(es6語法)。

    1、cd promise

    2、npm install bluebird


    查看全部
  • <!doctype><html>	<head>		<title>Promise?animation</title>		<style?type="text/css">			.ball?{				width:?40px;				height:?40px;				border-radius:?20px;			}			.ball1?{				background:?red;			}			.ball2?{				background:?yellow;			}			.ball3?{				background:?green;			}		</style>		<script?type="text/javascript"?src="./node_modules/bluebird/js/browser/bluebird.js"></script>	</head>	<body>		<div?class="ball?ball1"?></div>		<div?class="ball?ball2"?></div>		<div?class="ball?ball3"?></div>		<script?type="text/javascript">			var?ball1?=?document.querySelector('.ball1')			var?ball2?=?document.querySelector('.ball2')			var?ball3?=?document.querySelector('.ball3')			function?animate(ball,?distance,?cb)?{				setTimeout(function()?{					var?marginLeft?=?parseInt(ball.style.marginLeft,?10)					if(marginLeft?===?distance){						cb?&&?cb()					}else{						if(marginLeft?<?distance){							marginLeft++						}else{							marginLeft--						}						ball.style.marginLeft?=?marginLeft?+?'px'						animate(ball,?distance,?cb)					}				},?13)			}			/*animate(ball1,?100,?function()?{				animate(ball2,?200,?function()?{					animate(ball3,?300,?function()?{						animate(ball3,?150,?function()?{							animate(ball2,?150,?function()?{								animate(ball1,?150,?function()?{								})							})						})					})				})			})*/			var?promise?=?window.Promise			function?promiseAnimate(ball,?distance)?{				return?new?Promise(function(resolve,?reject)?{					function?_animate()?{						setTimeout(function()?{							var?marginLeft?=?parseInt(ball.style.marginLeft,?10)							if(marginLeft?===?distance){								resolve()							}else{								if(marginLeft?<?distance){									marginLeft++								}else{									marginLeft--								}								ball.style.marginLeft?=?marginLeft?+?'px'								_animate()							}						},?13)					}					_animate()				})			}			promiseAnimate(ball1,?100)				.then(function()?{					return?promiseAnimate(ball2,?200)				})				.then(function()?{					return?promiseAnimate(ball3,?300)				})				.then(function()?{					return?promiseAnimate(ball3,?150)				})				.then(function()?{					return?promiseAnimate(ball2,?150)				})				.then(function()?{					return?promiseAnimate(ball1,?150)				})		</script>	</body></html>


    查看全部
  • 用promise重構爬蟲代碼- new一個promise對象爬取每一節課程信息

    查看全部
  • 使用promise爬取多個頁面
    查看全部
  • Https服務器
    查看全部
  • Pipe左邊是可讀流,右邊是輸出流

    查看全部
  • Node.js 中有四種基本的流類型:


    Readable - 可讀的流 (例如 fs.createReadStream()).

    Writable - 可寫的流 (例如 fs.createWriteStream()).

    Duplex - (雙工流)可讀寫的流 (例如 net.Socket).

    Transform - (轉換流)在讀寫過程中可以修改和變換數據的 Duplex 流 (例如 zlib.createDeflate()).


    查看全部
  • Buffer的實例方法還有:

    pollsize:大?。?/p>

    isBuffer:判斷是否為buffer類型;

    compare:判斷相對位置;

    isEncoding:是否支持某種編碼;

    concat:連接創建為新的buffer對象;

    byteLength:獲得指定編碼下的字符串所占的字節數。


    查看全部
  • let http = require('https');

    let baseUrl = 'http://www.xianlaiwan.cn/learn/';

    let learnNumber_baseUrl = 'http://www.xianlaiwan.cn/course/AjaxCourseMembers?ids=';

    let cheerio = require('cheerio');

    let videosId = [728,637,348,259,197,134,75];


    function filerChapters(pageData){

    ? ? let html = pageData.html;

    ? ? let $ = cheerio.load(html);

    ? ? let chapters = $('.chapter');


    ? ? let courseData = {

    ? ? ? ? title:$('.hd h2').text(),

    ? ? ? ? number:pageData.number,

    ? ? ? ? id:$('.person-num').attr('href').split('/')[2],

    ? ? ? ? videos:[]

    ? ? };


    ? ? chapters.each(function(item){

    ? ? ? ? let chapter = $(this);

    ? ? ? ? let chapterTitle = chapter.find('h3').text();

    ? ? ? ? let videos = chapter.find('.video').children('li');

    ? ? ? ? let chapterData = {

    ? ? ? ? ? ? chapterTitle:chapterTitle,

    ? ? ? ? ? ? videos:[]

    ? ? ? ? };


    ? ? ? ? videos.each(function(item){

    ? ? ? ? ? ? let video = $(this).find('.J-media-item');

    ? ? ? ? ? ? let videoTitle = video.text().trim();

    ? ? ? ? ? ? let id = video.attr('href').split('video/')[1];

    ? ? ? ? ? ? let videoData = {

    ? ? ? ? ? ? ? ? title:videoTitle,

    ? ? ? ? ? ? ? ? id:id

    ? ? ? ? ? ? };


    ? ? ? ? ? ? chapterData.videos.push(videoData);

    ? ? ? ? });


    ? ? ? ? courseData.videos.push(chapterData);

    ? ? });


    ? ? return courseData;

    }


    function printCourseData(coursesData){

    ? ? coursesData.forEach(function(courseData){

    ? ? ? ? console.log('\n');

    ? ? ? ? console.log('? ? ?#########? ?'+courseData.title + '? [學習人數:' + courseData.number + ']? ?#########\n');

    ? ? ? ? courseData.videos.forEach(function(item){

    ? ? ? ? ? ? let chapterTitle = item.chapterTitle;

    ? ? ? ? ? ? console.log(chapterTitle );

    ? ? ? ? ? ? item.videos.forEach(function(video){

    ? ? ? ? ? ? ? ? console.log(' [' + video.id+ ']' + video.title.trim().split('(')[0]);

    ? ? ? ? ? ? });

    ? ? ? ? });

    ? ? });

    }


    function getPageAsync(url){

    ? ? return new Promise(function(resolve, reject){

    ? ? ? ? http.get(url,function(res){

    ? ? ? ? ? ? let html = '';

    ? ? ? ? ? ? res.on('data',function(data){

    ? ? ? ? ? ? ? ? html += data;

    ? ? ? ? ? ? });

    ? ? ? ? ? ? res.on('end',function(){

    ? ? ? ? ? ? ? ? resolve(html);

    ? ? ? ? ? ? });

    ? ? ? ? }).on('error',function(){

    ? ? ? ? ? ? console.log('error');

    ? ? ? ? });

    ? ? });

    }


    function getLearnDataAsync(html){

    ? ? return new Promise(function(resolve,reject){

    ? ? ? ? let $ = cheerio.load(html);

    ? ? ? ? let id = $('.person-num').attr('href').split('/')[2];

    ? ? ? ? let pageData = {

    ? ? ? ? ? ? html:html,

    ? ? ? ? ? ? number:0

    ? ? ? ? };


    ? ? ? ? let db = '';

    ? ? ? ? http.get(learnNumber_baseUrl+id,function(res){

    ? ? ? ? ? ? res.on('data',function(data){

    ? ? ? ? ? ? ? ? db += data;

    ? ? ? ? ? ? ? ? db = JSON.parse(db);

    ? ? ? ? ? ? ? ? pageData.number = parseInt(db.data[0].numbers,10);

    ? ? ? ? ? ? });


    ? ? ? ? ? ? res.on('end',function(){

    ? ? ? ? ? ? ? ? resolve(pageData);

    ? ? ? ? ? ? });


    ? ? ? ? }).on('error',function(){

    ? ? ? ? ? ? console.log('error');

    ? ? ? ? });

    ? ? });

    }


    let promiseList = [];

    // let coursesDataPromises = [];


    videosId.forEach(function(id){

    ? ? promiseList.push(getPageAsync(baseUrl+id).then(function(html){

    ? ? ? ? return getLearnDataAsync(html);

    ? ? }));

    });


    Promise

    ? ? .all(promiseList)

    ? ? .then(function(pagesData){

    ? ? ? ? let coursesData = [];

    ? ? ? ? pagesData.forEach(function(pageData){

    ? ? ? ? ? ? coursesData.push(filerChapters(pageData));

    ? ? ? ? });


    ? ? ? ? printCourseData(coursesData);

    ? ? });


    查看全部
  • (一)Promise 1. ES6的Promise語言標準 2. Promise/A+規范 (二)Promise使用場景 1. 是一種異步的實踐方案 2. 特別是Callback Hell, 可以用同步的方式寫異步代碼 (三) Promise的三種狀態 1. pending ?未完成 2. fulfilled 已完成 3. rejected 失敗 (1->2, 1->3 正確) (2->1, 3->1, 2->3 錯誤) 總結: 只能又未完成變為已完成或失敗, 且不可逆, 改變只能一次, 不存在即已完成同時失敗

    查看全部
  • 說一下我的理解,Promise沒有把異步變同步,只是以同步的方式來寫異步,使用promise,當代碼執行到resolve時跳到下一步的then方法中依次執行,執行到reject時跳到catch方法依次執行;上一步then方法中返回的值可以是一個新的Promise也可以是某一固定值,為新的Promise時會根據其resolve和reject來進行下一步的代碼執行,當為固定值時會把該值傳給下一步的then方法參數使用。
    Ajax解決的是網頁異步刷新問題,完全可以在Promise中嵌套使用ajax。

    查看全部

舉報

0/150
提交
取消
課程須知
本課程是一個系列課程,前導課程是《進擊 Node.js 基礎(一)》,所以建議小伙伴們學習本課程之前先把它拿下。
老師告訴你能學到什么?
1、了解 Promise 2、橫掃 Nodejs API:Buffer、API-Stream

微信掃碼,參與3人拼團

微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號

友情提示:

您好,此課程屬于遷移課程,您已購買該課程,無需重復購買,感謝您對慕課網的支持!