HTTP是一种协议,计算机要共同听从这种规则,才能相互通讯。html
浏览器打开一个网站,http走过的环节node
4.1宽带运营商服务器查看自己缓存-》npm
4.2运营商服务器发起一个迭代DNS解析的请求(根域->顶级域->域名注册商(IP地址)) 编程
4.3运营商服务器把结果返回给操做系统内核同时缓存起来 后端
4.4操做系统内核把结果返回给浏览器 数组
HTTP的组成:浏览器
http请求方法: 缓存
状态码:服务器端返回浏览器,告知浏览器请求成功或失败的信息 服务器
回调试异步编程时的基础,将后续逻辑封装成起始函数的参数,逐层嵌套多线程
function learn(something){ console.log(something) } function we(callback, something){ something += 'is cool' callback(something) } //传入具名函数 we(learn, 'Nodejs') //传入匿名函数 we(function(something){ console.log(something) }, 'Jade')
同步:发送方发送数据后,等待接收方发回响应之后才发下一个数据包的通信方式
异步:发送方发出数据后,不等接收方发回响应,接着发送下个数据包的通信方式
磁盘的写入(in)磁盘的读取(out)
一次只能执行一个程序叫作单线程 一次能执行多个程序叫作多线程
阻塞:前一个程序未执行完就得一直等待
非阻塞:前一个程序未执行完时能够挂起,继续执行其余程序,等到使用时再执行
一个触发动做(例如点击按钮)
一个触发动做引发的操做(例如点击按钮后弹出一个对话框)
function clickIt(e){ window.alert('Button is clicked') } var button = document.getElementById('#button') button.addEventListener('click', clickIt)
为了某个事件注册了回调函数,可是这个回调函数不是立刻执行,只有当事件发生的时候,才会调用回掉函数,这种函数执行的方式叫作事件驱动~这种注册回调就是基于事件驱动的回调,若是这些回调和异步I/O(数据写入、读取)操做相关,能够看做是基于回调的异步I/O。只不过这种回调在nodejs中是有事件来驱动的
事件循环Eventloop,假若有大量的异步操做,一些I/O的耗时操做,甚至是一些定时器控制的延时操做,它们完成的时候都要调用相应的回调函数,而从完成一些密集的任务,而又不会阻塞整个程序执行的流程,此时须要一种机制来管理,这种机制叫作事件循环 总而言之,管理大量异步操做的机制叫作事件循环
EventLoop: 回调函数队列,异步执行的函数会被压入这个队列;队列被循环查询。
与调用函数,访问变量的能力有关。
做用域分为:局部和全局(在局部做用域里能够访问到全局做用域的变量,但在局部做用域外面就访问不到局部做用域里面所设定的变量)
//定义一个全局变量 var globalVariable = 'This is global variable' //定义一个全局做用域 function globalFunction(){ //在全局做用域里定义一个局部变量 var localVariable = 'This is local variable' //打印出全局变量和局部变量 console.log('Visit global/loacal variable') console.log(globalVariable) console.log(localVariable) //改变全局变量 globalVariable = 'This is changed variable' //打印已经改变的全局变量 console.log(globalVariable) //在全局做用域里定义一个局部函数 function localFunction(){ //在局部做用域里定义一个局部变量 var innerLocalVariable = 'This is inner local variable' //打印全局变量,局部变量,内部局部变量 console.log('Visit global/local/innerLocal variable') console.log(globalVariable) console.log(localVariable) console.log(innerLocalVariable) } localFunction() } globalFunction() //输出结果 //Visit global/loacal variable //This is global variable //This is local variable //This is changed variable //Visit global/local/innerLocal variable //This is changed variable //This is local variable //This is inner local variable
与this关键字有关,是调用当前可执行的代码的引用,this老是指向调用这个方法的对象
js里的this一般是当前函数的拥有者 this是js的一个关键字,表明函数运行时自动生成的一个内部对象,只能在函数内部使用
var pet ={ words: '...', speak: function(){ console.log(this.words)// '...' console.log(this == pet)//true } } //speak()方法里的this指向拥有speak()方法的对象pet, //因此 this == pet 为true pet.speak()
function pet(words){ this.words = words; console.log(this.words) // '...' console.log(this === gloabal)//true } pet('...')
function Pet(words){ this.words = words this.speak = function(){ console.log(this.words) //Miao console.log(this)//打印出this指向的整个实例化对象,{words: 'Miao',speak: [Function]} } } var cat = new Pet('Miao') cat.speak() //speak()里面的this指向cat
三、更改上下文方法
(更改this指向的内容,可方便地实现继承):
二者的做用彻底相同,只是接收参数的方式不太同样
都是为了改变某个函数运行时的context即上下文而存在的,换句话说,就是为了改变函数内部this的指向。
var Pet = { words: '...0', speak: function(say){ console.log(say+ ' ' +this.words) } } pet.speak('Speak') //Speak ... var dog = { words: 'Wang' } pet.speak.call(dog, 'Speak') // Speak Wang //call()将pet.speak指定的原来的对象pet转换为指向对象dog
call()和apply()实现继承
function Pet(words){ this.words = words this.speak = function(){ console.log(this.words) } } function Dog(words){ Pet.call(this,words) //直接把this转化为指向Dog对象,实现了Dog继承了Pet } var dog = new Dog('Wang') dog.speak() //Wang
慕课网Nodejs基础入门课程标题爬虫
var http = require('http') var cheerio = require('cheerio') //将cheerio模块require进来 /*添加cheerio模块,就像jQuery同样操做装载后的html, 先安装该模块,在cmd命令行中输入:npm install cheerio -g */ var url = 'http://www.imooc.com/learn/348' //找到课程页面,只爬单页面 function filterChapters(html){ var $ = cheerio.load(html) //经过cheerio.load()将html内容装载进来 var chapters = $('.chapter') //先拿到每一大章 // 想要获得每一章的内容是一个数组 //[{ // chapterTitle: ' ', // videos: [ // title: ' ', // id: ' ' // ] // }] var courseData = [ ] chapters.each(function(item){ var chapter = $(this) var chapterTitle = chapter.find('strong').text() var videos = chapter.find('.video').children('li') //每一章就是一个对象自变量 var chapterData = { chapterTitle : chapterTitle, videos: [ ] //该videos如今仍是一个空数组 } //对每个videos进行遍历,拿到单个video videos.each(function(item){ var video = $(this).find('.J-media-item') var videoTitle = video.text() var id = video.attr('href').split('/video')[1]//split()方法是将指定字符串按某指定的分隔符进行拆分,拆将会造成一个字符串的数组并返回 chapterData.videos.push({ //将videoTitle和idpush进videos[ ]中 title: videoTitle, id : id }) }) courseData.push(chapterData) //将chapterData数组push进courseData中 }) return courseData //返回回去courseData给filterChapters } function printCourseInfo(courseData){ courseData.forEach(function(item){//由于courseData是一个数组,因此须要遍历该数组后打开 var chapterTitle = item.chapterTitle console.log(chapterTitle+ '\n') //将chapterTitle打印出来 item.videos.forEach(function(video){ console.log(' 【' +video.id +' 】' + video.title +'\n') //遍历videos后将其打印出来 }) }) } http.get(url, function(res){ //第一个参数传入url,第二个参数是回调的方法 var html = ' ' res.on('data', function(data){ //response由data触发的时候,回调data,把html的内容被拼加进来 html += data }) res.on('end', function(){ //数据累加,最后输出 var courseData = filterChapters(html) //将html做为参数传递给filterChapters,让该函数去作数据的过滤 printCourseInfo(courseData) //经过printCourseInfo函数打印 }) }).on('error', function(){ //抛出异常,当出现错误的时候 console.log('获取课程数据出错!') })
其中,在安装cheerio模块时出现了错误,缘由是没有全局变量安装,使用 nmp install cheerio-g
或,选择配置环境变量解决!
var EventEmitter = require('events').EventEmitter;
var instance = new EventEmitter();
instance.on('eventName',function(){});
//自定义最大数
每一个setMaxListeners针对的是一个特定事件,设置最大的num
instance.setMaxListeners(num)
instance.emit('eventName',arguments)
boolean.instance.emit('eventName',arguments) //true or false
instance.moveListener('eventName',funcName)//移除事件须要具名函数,匿名函数不可移除
instance.removeAllListener()//表示移除全部时间的监听
instance.moveAllListener('eventName')//移除特定的event的全部事件
var EventEmitter = require('events').EventEmitter //注意是events! var life = new EventEmitter() //生成实例 life.setMaxListeners(11) function water(which){ console.log('The'+which+'is showing...') } life.on('date', water) //为该实例添加监听 life.on('date', function(which){ console.log('The'+which+'is down...') }) life.on('date', function(which){ console.log('The'+which+'is funny...') }) life.on('date', function(which){ console.log('The'+which+'is cool...') }) life.on('date', function(which){ console.log('The'+which+'is dark...') }) life.on('date', function(which){ console.log('The'+which+'is light...') }) life.on('date', function(which){ console.log('The'+which+'is wonderful...') }) life.on('date', function(which){ console.log('The'+which+'is interesting...') }) life.on('date', function(which){ console.log('The'+which+'is sad...') }) life.on('date', function(which){ console.log('The'+which+'is happy...') }) life.on('date', function(which){ console.log('The'+which+'is boring...') }) /*//判断事件是否有被监听过? var hasFirstListener = life.emit('date','movies') var hasSecondListener = life.emit('date1','dinner') var hasThirdListener = life.emit('date2','shopping') console.log(hasFirstListener) console.log(hasSecondListener) console.log(hasThirdListener)*/ //事件超过十个,出现警告。可设置setMaxListener life.on('date1',function(which){ console.log('The'+which+'dilicious') }) life.on('date1',function(which){ console.log('The'+which+'bad') }) //移除监听事件时,只能移除具名函数 life.removeListener('date',water) //移除全部的监听事件 life.removeAllListeners('date1')//须要传入具体事件名称才不会所有删除致使出错 var hasFirstListener = life.emit('date','movies') var hasSecondListener = life.emit('date1','dinner') //查看监听事件的个数 console.log(life.listeners('date').length) //移除掉一个后只剩10个 console.log(EventEmitter.listenerCount(life,'date'))