美文网首页
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.计时器

    相关文章

      网友评论

          本文标题:nodejs学习笔记

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