node of knowledge

(A) Node can solve the problem?
1) Node's primary goal is to provide a simple, high-performance server for creating development tools
2) implementation of Java and Php contrast, the Web server bottleneck is that the amount of concurrent users
3) the Node in the treatment of high concurrency, I / O-intensive scene has significant performance advantages
  1) high concurrency, means at the same time concurrent access server
  2) I / O intensive refers to file operations, network operations, a database
  3) I / O intensive opposite is CPU-intensive, CPU-intensive refers to a logical processing operation, compression, decompression, encryption, decryption
. 4) Web major scene is to receive request from the client, reads the static resources and rendering the interface, so the Node very suitable Web application development

 

2) Node background appears
to address the high concurrency Web server performance issue
1) What are performance issues?
  

  1) in response to the transmission of shortened length
    1) faster transmission request
    can not be controlled (client decision speed)
    2) faster in response
    to control (increase the bandwidth of the server, CDN acceleration ...)
    3) processing the request server tasks faster
    can control (good programmer)
    4) the server reads from disk / write data to the database faster
      all post-project will encounter bottlenecks
    can not control (the reading speed of the disk has an upper limit)

....
  2) traditional server operational processes
    multi-threaded
    sending a request to open a thread
    for example: waiters
    requires a lot of resources to support
2) Ryan Dahl (Ryan Dahl) tried to use Ruby, c, Lua to solve, but all because of various restrictions and language itself unstuck
leopard change its spots
language historical burden is too heavy, ship does not turn around
in various languages are deep-rooted ideas, can not easily change the ecology
3) appear V8 engine,
  gradually groped key to solve the problem of
  event-driven
  asynchronous I / O

 

Appears 3) V8 engine
  1) What is the V8 engine?
  Section devoted to the JavaScript language interpretation and execution of virtual machine processes
  such as embedding V8 engine into the browser, so we write JavaScript code will be executed by the browser ; then if the V8 engine embedded into NodeJS environment, then we write JavaScript code will be executed by the server
  when the V8 engine embedded in different host environments, the JavaScript language can be applied to different areas of multi-
  2) initial the role?
  for Chrome browser parses js script
  such as: sending an HTTP request to the server, the server returns the HTTP response to requests
  3) the advantage V8 engine?
    1) compiled a strong and rapid execution efficiency of
    the use of a large number of algorithms and clever wicked craft
    2) performance is very good, it's far more than the efficiency of Python, Ruby and other scripting languages
    3) historical burden light, there is no synchronization the I / O
    4) powerful event-driven mechanism
    4) Node birth of Ryan Dahl (Ryan Dahl ) modified V8 engine core, put it in the server development, after such a thing modified is called Node.js

 

(C) Node.js Introduction
1) What is Node.js
  1) Node.js is based on the Chrome V8 JavaScript engine operating environment (Runtime)
  1) the Node is not a language, but a development tool that allows to run JS backend
  2) Node does not include the complete works of JavaScript, DOM and BOM is not on the server side
  3) Node also provides a series of new modules, such as: http, fs modules, etc.
  before 4) Node, js code can only run on the client, can only badly in the browser
  after 5) Node, js code can and operating system (Mac OS, windows, Linux ... ) interaction battlefield extends from the browser to the server
  6) changes in the version
  initially called Web.js, the purpose is to write high-performance Web server for
  the write bigger, formation of ecological (server development, rely on a variety of frameworks ...), renamed Node.js
  the node: node, node open source team hopes it can be like the same node continues to expand, grow
  icon

  

 

2) The other back-end functions like language

  PHP, JSP, Python, Ruby
  and systems interact
difference 3) and other back-end languages
  1) Node.js is not an independent language
    1) PHP, JSP, .... not only a language, but also the platform
    2) Node. js carried out with JavaScript programming, operating platform is the packaged js engine (V8)
  2) lightweight architecture
    1) java, php, .net need to run on the server, the Apache, toMat, nginx, IIS
    2) Node.js bridged over without any server software
    3) with minimal hardware cost, achieve higher concurrency, better processing performance
    4) Node.js use of event-driven, non-blocking I / O model, so that the amount of light and efficient; and Node.js package manager npm, the world's largest repository of open source ecosystem

2) processes and threads 1) the concept of
  the basic unit 1) process is the operating system resource allocation and scheduling tasks
  2) threading process is based on a program run unit, a process can have multiple threads
 2) is a single browser thread or multi-threaded?
  1) illustrates
    

  3)浏览器是多线程的,我们更关注的是浏览器的渲染引擎
3)渲染引擎
  1)渲染引擎内部是多线程的,内部包含两个最为重要的线程ui线程和js线程
  2)ui线程和js线程是互斥的,因为JS运行结果会影响到ui线程的结果
  3)ui更新会被保存在队列中等到js线程空闲时立即被执行
4)JS是单线程的
  1)JS在最初设计时就设计成了单线程,因为当时是用于UI绘制,多个线程同时操作DOM会很混乱
  2)这里所说的单线程指的是主线程是单线程的,所以在Node中主线程依旧是单线程的
  3)其他线程
    1)浏览器事件触发线程(用来控制事件循环,存放setTimeout、浏览器事件、ajax的回调函数)
    2)定时触发器线程(setTimeout定时器所在线程)
    3)异步HTTP请求线程(ajax请求线程

  4)补充
    1)单线程特点是节约了内存,并且不需要再切换执行上下文
    2)而且单线程不需要管锁的问题
    例如:下课了大家都要去上厕所,厕所就一个大号,相当于所有人都要访问同一个资源,那么先进去的就要上锁
    而对于node来说,下课了就一个人去厕所,所以免除了锁的问题

  5)查看线程
    右击 任务栏 -->任务管理器 --> 进程



3)事件循环
  1)图示

  2)运行原理
    1)所有同步任务都在主线程上执行,形成一个执行栈
    2)主线程之外,还存在一个任务队列;只要异步任务有了运行结果,就在任务队列之中放置一个事件
    3)一旦执行栈中的所有同步任务执行完毕,系统就会读取任务队列,将队列中的事件放到执行栈中依次执行
    4)主线程从任务队列中读取事件,这个过程是循环不断的
4)Node运行过程
  1)图示

   2)运行过程
    1)我们写的js代码会交给v8引擎进行处理
    2)代码中可能会调用nodeApi,node会交给libuv库处理
    3)libuv通过阻塞i/o和多线程实现了异步io
    4)通过事件驱动的方式,将结果放到事件队列中,最终交给我们的应用
  3)同步与异步
    同步和异步关注的是消息通知机制
    2)同步
      同步就是发出调用后,没有得到结果之前,该调用不返回,一旦调用返回,就得到返回值了
      调用者主动等待这个调用的结果
    3)异步
      异步则相反,调用者在发出调用后这个调用就直接返回了,所以没有返回结果
      当一个异步过程调用发出后,调用者不会立刻得到结果,而是调用发出后,被调用者通过状态、通知或回调函数处理这个调用
  4)阻塞与非阻塞
    阻塞和非阻塞关注的是程序在等待调用结果(消息,返回值)时的状态
    2)阻塞调用
      指调用结果返回之前,当前线程会被挂起
      调用线程只有在得到结果之后才会返回
    3)非阻塞调用
      在不能立刻得到结果之前,该调用不会阻塞当前线程
2)Node.js的特点
  1)单线程
    1)优势
    减少了内存开销(操作系统完全不再有线程创建、销毁的时间开销)
    Node.js不为每个客户连接创建一个新的线程,而仅仅使用一个线程
    2)劣势
    如果某一个事情,进入了,但是被I/O阻塞了,整个线程就阻塞了
    如果一个人把Node.js主线程搞崩溃,全部崩溃(但很难搞崩溃)
  2)非阻塞I/O
    1)基本概念?
    当在访问数据库取得数据的时候,需要一段时间。在传统的单线程处理机制中,在执行了访问数据库代码之后,整个线程都将暂停下来,等待数据库返回结果,才能执行后面的代码。也就是说,I/O阻塞了代码的执行,极大地降低了程序的执行效率
  不会傻等I/O语句结束,而会执行后面的语句
  Node.js中采用了非阻塞型I/O机制,因此在执行了访问数据库的代码之后,将立即转而执行其后面的代码,把数据库返回结果的处理代码放在回调函数中,从而提高了程序的执行效率
  当某个I/O执行完毕时,将以事件的形式通知执行I/O操作的线程,线程执行这个事件的回调函数。为了处理异步I/O,线程必须有事件循环,不断的检查有没有未处理的事件,依次予以处理
  阻塞模式下,一个线程只能处理一项任务,要想提高吞吐量必须通过多线程。而非阻塞模式下,一个线程永远在执行运行操作,这个线程的CPU核心利用率永远是100%
  所以,这是一种特别有哲理的解决方案:与其人多,但是好多人闲着;还不如一个人玩命,往死里干活儿
  2)非阻塞就能解决问题了么?
    比如执行着小红的业务,执行过程中,小刚的I/O回调完成了,此时怎么办??所以要有事件驱动循环
  3)事件驱动
  1)基本概念?
    不管是新用户的请求,还是老用户的I/O完成,都将以事件方式加入事件环,等待调度
  2)运作流程?

    1)在Node中,客户端请求建立连接,提交数据等行为,会触发相应的事件
    2)在Node中,在一个时刻,只能执行一个事件回调函数, 但是在执行一个事件回调函数的中途,可以转而处理其他事件(比如,又有新用户连接了),然后返回继续执行原事件的回调函数,这种处理机制,称为“事件环”机制。
    3)当某一个事件发生的时候,就去执行回调函数。执行完毕之后,再去找到事件循环当中找一个新的事件进行来
    4)Node.js当中所有的I/O都是异步的, 都是回调函数套回调函数

 

 

3)实践2
  1)模块标识
    当我们使用require()引入外部模块时,使用的就是模块标识,我们可以通过模块标识来找到指定的模块
    比如: let myFunc = require("./js/myFunc");
  2)思考: export和require怎么来的?
    1)错误答案: 全局变量
      1)window不是Node中的全局对象
      2)Node中有一个全局对象global, 作用和window类似
    2)正确答案: 函数参数
      1)函数的标识: arguments
        获取函数的所有实参
      2)获取函数自身 arguments.callee
        返回函数本身
3)node文件组成剖析
  1)当node在执行模块中的代码时,它会首先在代码的最顶部,添加如下代码 function (exports, require, module, __filename, __dirname) {
  2) 在代码的最底部,添加 }
  3)所以模块中的代码都是包装在一个函数中执行的,并且在函数执行的同时传递进了5个实参
    exports: 该对象用来将函数内部的局部变量或局部函数暴露到外部
    require: 用来引入外部的模块
    module: 代表的是当前模块本身, exports就是module的属性; 我们既可以使用 exports 导出,也可以使用module.exports导出
    __filename: 当前模块的完整路径
    __dirname: 当前模块所在文件夹的完整路径

Guess you like

Origin www.cnblogs.com/zhangzhengyang/p/11111939.html