简体   繁体   English

Node.js框架和Express.js

[英]Node.js framework & Express.js

What are the best resources to learn Express.js? 学习Express.js的最佳资源是什么? Can anybody explain the node.js framework,how exactly it works. 任何人都可以解释一下node.js框架,它是如何工作的。

The nonblocking eventloop concept. 非阻塞事件循环的概念。

I've found the Express website explains things pretty well, and Express to be quite approachable for new users. 我发现Express网站可以很好地说明问题,而Express对于新用户来说很容易上手。

A multi-threaded system (Java and underlying JVM, for instance), contains many threads of execution that may each execute its own code instructions simultaneously (on a multi-core hardware CPU), or switched between, where each thread runs for a scheduled period of time, and then the OS schedules the next thread for execution. 多线程系统(例如Java和底层JVM)包含许多执行线程,每个执行线程可以同时(在多核硬件CPU上)同时执行自己的代码指令,也可以在每个线程之间按计划运行它们之间进行切换时间段,然后操作系统安排下一个线程执行。

Node programs are executed in the Node environment, which is single threaded, so there is only a single thread of code execution for the entire program and no multiple threads executing concurrently. 节点程序在单线程的节点环境中执行,因此整个程序只有一个代码执行线程,没有多个线程可以同时执行。

A simple analogy would be comparing the event loop with a standard programming construct, the while loop, which is exactly what it is. 一个简单的类比就是将事件循环与标准编程结构while循环进行比较,而这正是它的本质。

while(1){
   // Node sets this up.  Do stuff.. Runs until our program terminates.
}

Starting a node program would start this loop. 启动节点程序将启动此循环。 You could imagine your program being inserted into this loop. 您可以想象您的程序将插入到此循环中。

If the first instruction in your program was to read a file from disk. 如果程序中的第一条指令是从磁盘读取文件。 That request would be dispatched to the underlying OS system call to read the file. 该请求将分派给底层OS系统调用以读取文件。

Node provides Asynchronous and Synchronous functions for things like reading a file, although the asynchronous is generally preferred because in a synchronous call, a problem with reading the file halts the entire program, in a single threaded system. 尽管通常首选异步,因为在同步调用中,读取文件的问题会在单个线程系统中使整个程序停止,但Node会为读取文件之类的事情提供异步和同步功能。

while(1){
  require('fs').readFileSync('file.txt');
  // stop everything until the OS reports the file has been read
}

In the (preferred) asynchronous version, the request to read the file is issued to the OS, and a callback function is specified, the loop continues. 在(首选)异步版本中,向OS发出读取文件的请求,并指定了回调函数,循环继续进行。 The program essentially waits for the OS to respond, and on the next loop (aka tick), your provided callback function (essentially just a location in memory) is called by the system with the result. 该程序实质上是在等待操作系统响应,然后在下一个循环(又称滴答)中,系统将使用结果调用您提供的回调函数(实际上只是内存中的一个位置)。

while(1){
  // 1st loop does this
  require('fs').readFile('file.txt', callback);

  // 2nd loop does this, system calls our callback function with the result
  callback(err, result)
}

There are anticipated advantages of a single threaded system. 单线程系统具有预期的优势。 One is that there is no context switching between threads that needs to be done by the OS, which removes the overhead of performing that task in the system. 一种是操作系统无需在线程之间进行上下文切换,从而消除了在系统中执行该任务的开销。

The other, and this is a hotly debated topic of how this compares against the way other systems and programming languages handle it - is the simplicity of programming using callback functions as a means to implement asynchronicity. 另一个是如何将它与其他系统和编程语言进行比较的一个热门话题,那就是使用回调函数作为实现异步性的方法来简化编程。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM