美文网首页
nodejs学习笔记

nodejs学习笔记

作者: 前端混合开发 | 来源:发表于2019-08-21 22:13 被阅读0次

什么是中间件
简单说,中间件(middleware)就是处理HTTP请求的函数。它最大的特点就是,一个中间件处理完,再传递给下一个中间件。App实例在运行过程中,会调用一系列的中间件。
express.static指定了静态页面的查找目录


Nodejs --> server side framework: event-driven model.
Express --> webserver; module;
Angularjs --> client side framework
用nodejs做服务器端端好处:
1. 服务端和客户端都可以用js来写;
2. 单线程的;
3. 可扩展:可以写自己的库;
4. 方便安装;
Ndoe.js applications run in a single-threaded event-driven model. Although Node.js implements a thread pool in the background to do work, the application itself doesn't have any concept of multiple threads.

Node.js adds work to an event queue and then has a single thread running an event loop pick it up. The event loop grabs the top item in the event queue, executes it, and then grabs the next item. When executing code that is longer lived or has blocking I/O, instead of calling the function directly, it adds the function to the event queue along with a callback that will be executed after the function completes. When all events on the Node.js event queue have been executed, the Node.js application terminates.




It adds the GetFille and GetData requests to the event queue. It first picks up the GetFile request , executes it, and completes by adding the open() callback function to the event queue. Then it picks up the GetData request, executes it, and completes by adding the Connect() callback function to the event queue. This continues until there are no callback functions to be executed. Figure 4.2 : the events for each thread don't necessarily follow a direct interleaved order. For example, the connect request takes longer to complete than does the read request,
So send(file) is called before Query(db).

Some examples of blocking I/O are:
Reading a file;
Querying a database;
Requesting a socket;
Accessing a remote service;
Node.js uses event callback to avoid having to wait for blocking I/O;

你在一个party上,扮演webserver的角色,所有的对话就是你要处理的任务。
You are acting the part of the webserver, and the conversations represent the work necessary to process different types of web requests. Your conversations are broken up into several segments with different individuals. You end up talking to one and then another, then back to the first and then a third, back to the second, and so on.

In Node.js applications, you schedule work on the event queue by passing a callback function using one of these methods:

  • a. Make a call to one of the blocking I/O library calls, such as writing to a file or connecting to a database;
  • b. 事件监听Add an event listener to a built-in event such as an http.request or server .connection.
  • c. Create your own event emitters and add custom listeners to them.
  • d. Using the process.nextTick option to schedule work to be picked up on the next cycle of the event loop.
  • e. Use timers to schedule work to be done after a particular amount of time or at periodic intervals.计时器

相关文章

  • 2018-08-21nodejs

    Nodejs学习笔记 一、 NodeJs介绍 什么是NodeJS,在应用程开发中起什么作用? Nodejs是一个应...

  • Nodejs学习笔记-Nodejs介绍

    什么是Node.js 编写高性能网络服务器的JavaScript工具包(用js开发服务端程序)单线程、异步、事件驱...

  • nodejs学习笔记

    JavaScript模块编译 在编译过程中,node对获取的JavaScript文件内容进行了头尾包装。正常的Ja...

  • nodejs学习笔记

    模块 名词解释:每一个js文件就是一个模块,而文件路径就是模块名。每个模块(也就是每个js文件)都有requir,...

  • Nodejs学习笔记②

    写在前面 这次做一个小小的登陆&注销登陆功能练习下所学的知识,并扩充些新知识。 目录 新建 login 项目 下载...

  • Nodejs学习笔记①

    写在前面 undefined 目录 检查更新node&npm版本 安装Express 4.x Express 4....

  • nodejs学习笔记

    Node.js 是一个基于 Chrome V8 引擎的 JavaScript 运行环境。Node.js 使用了一个...

  • Nodejs学习笔记

    Node.js 是单进程单线程应用程序,但是通过事件和回调支持并发,所以性能非常高。nodejs由以下模块组成:引...

  • NodeJS 学习笔记

    NodeJS使用 CommonJS 模块系统。整个项目都是由一个个模块组成的,模块的存在形式是文件,他们一一对应。...

  • Nodejs学习笔记

    以前学习C、OC、Swift、H5的时候都没有留下痕迹,心里甚是遗憾,最近用Nodejs参与了一个web开发,果断...

网友评论

      本文标题:nodejs学习笔记

      本文链接:https://www.haomeiwen.com/subject/rbywsctx.html