-
var options = {
????key: fs.readFileSync('ssh_key.pem'),
????cert: fs.readFileSync('ssh_cert.pem')
}
https
????.createServeer(options, (req, res) => {
????????res.writeHead(200);
????????res.end('Hello');
????})
????.listen(8080)
查看全部 -
http和https的區(qū)別。
https在http的基礎(chǔ)上,增加了SSL/TLS(數(shù)據(jù)加密傳輸)
查看全部 -
$(dom).trim() //去掉多余空格
查看全部 -
記錄一下promise庫,其實(shí)平時(shí)都是手寫用的比較多
查看全部 -
buffer實(shí)例方法
查看全部 -
var stream=require('stream');
var util=require('util');
var ReadStream=function(){
stream.Readable.call(this);
}
util.inherits(ReadStream,stream.Readable);
ReadStream.prototype._read=function(){
this.push('i');
this.push('love');
this.push('you');
this.push(null);//必須要的,不然會(huì)一直重復(fù)輸入
}
var WriteStream=function(){
stream.Writable.call(this);
this._cached=new Buffer('');
}
util.inherits(WriteStream,stream.Writable);
WriteStream.prototype._write=function(chunk,encode,cb){
console.log(chunk.toString());
cb();
}
var TransformStream=function(){
stream.Transform.call(this);
}
util.inherits(TransformStream,stream.Transform);
TransformStream.prototype._transform=function(chunk,encode,cb){
this.push(chunk);
cb();
}
TransformStream.prototype._flush=function(cb){
this.push('really');
cb();
}
new ReadStream().pipe(new TransformStream).pipe(new WriteStream);
查看全部 -
var fs=require('fs');
fs.readFile('logo.png',function(err,originBuffer){
console.log(Buffer.isBuffer(originBuffer));
fs.writeFile('logo_buffer.png',originBuffer,function(err){
if(err) console.log(err);
});
var base64Image=originBuffer.toString('base64');
console.log(base64Image);
var buf=new Buffer(base64Image,'base64');
console.log(Buffer.compare(buf,originBuffer));
});
查看全部 -
使用cheerio的find來找到節(jié)點(diǎn)時(shí),如果class有兩個(gè)類名組成,只能使用第一個(gè),使用第二個(gè)找不到數(shù)據(jù)的,如果使用了數(shù)組的話記得加上$(),如$(find('.meta-value')[1]).text();因?yàn)榧由?()才能作為一個(gè)節(jié)點(diǎn)對(duì)象使用。
查看全部 -
var http=require('http');
var cheerio=require('cheerio');
var Promise=require('bluebird');
var bashUrl='http://idcbgp.cn/learn/'
var courseIds=[348,637];
//過濾數(shù)據(jù)
function filterChapters(html){
var $=cheerio.load(html);
var chapters=$('.chapter');
var courseData=[];
var courseTitle=$('.hd').find('h2').text().replace(/\s/g,'');
var courseNumber=$($('.static-item')[2]).find('.meta-value').text().replace(/\s/g,'');
chapters.each(function(index,value){
var chapter=$(this);
var chapterTitle=chapter.find('h3').text().replace(/\s/g,'');
var videos=chapter.find('.video').children('li');
var chapterData={
chapterTitle:chapterTitle,
videos:[]
}
videos.each(function(index,value){
var video=$(this);
var videoTitle=video.text().replace(/\s/g,'').split('開始學(xué)習(xí)')[0];
var id=video.find('a').attr('href').split('/')[2].replace(/\s/g,'');
var videoData={
title:videoTitle,
id:id
}
chapterData.videos.push(videoData);
});
courseData.push(chapterData);
});
var courseObjectData={
courseTitle:courseTitle,
courseNumber:courseNumber,
courseData:courseData
}
return courseObjectData;
}
//將得到的數(shù)據(jù)展示出來
function showCourseData(coursesObjectData){
coursesObjectData.forEach(function(courseObjectData){
console.log('課程名稱:'+courseObjectData.courseTitle+'\n');
console.log('學(xué)習(xí)人數(shù):'+courseObjectData.courseNumber+'\n\n');
courseObjectData.courseData.forEach(function(item){
var chapterTitle=item.chapterTitle;
console.log(chapterTitle+'\n');
item.videos.forEach(function(item){
console.log('【'+item.title+'】'+'\n');
console.log('視頻號(hào):'+item.id+'\n');
});
});
});
}
/*
使用http模塊來得到html文檔
*/
function promiseGet(url){
return new Promise(function(resolve,reject){
http.get(url,function(res){
var html='';
res.on('data',function(data){
html+=data;
});
res.on('end',function(){
resolve(html);
});
res.on('error',function(e){
reject(e);
});
});
});
}
var promiseCourseObjectData=[];
courseIds.forEach(function(id){
promiseCourseObjectData.push(promiseGet(bashUrl+id));
});
Promise.all(promiseCourseObjectData).then(function(pages){
var coursesObjectData=[];
pages.forEach(function(html){
var courseObjectData=filterChapters(html);
coursesObjectData.push(courseObjectData);
});
showCourseData(coursesObjectData);
});
查看全部 -
(一)Promise 1. ES6的Promise語言標(biāo)準(zhǔn) 2. Promise/A+規(guī)范 (二)Promise使用場景 1. 是一種異步的實(shí)踐方案 2. 特別是Callback Hell, 可以用同步的方式寫異步代碼 (三) Promise的三種狀態(tài) 1. pending ?未完成 2. fulfilled 已完成 3. rejected 失敗 (1->2, 1->3 正確) (2->1, 3->1, 2->3 錯(cuò)誤) 總結(jié): 只能又未完成變?yōu)橐淹瓿苫蚴? 且不可逆, 改變只能一次, 不存在即已完成同事失敗
查看全部 -
111
查看全部 -
轉(zhuǎn)化流的_flush事件查看全部
-
讀寫操作查看全部
-
各種事件查看全部
-
Buffer靜態(tài)方法 poolSize isBuffer compare isEncoding concat byteLength查看全部
舉報(bào)