官术网_书友最值得收藏!

Node.js

Node.js? is a JavaScript runtime built on Chrome's V8 JavaScript engine. Node.js uses an event-driven, non-blocking I/O model that makes it lightweight and efficient. The Node.js package ecosystem, npm (https://www.npmjs.com/), is the largest ecosystem of open source libraries in the world. Node.js is a platform for building fast and scalable server applications using JavaScript. We will be using Node.js when we write serverless applications in later chapters, which we will use to deploy to AWS, Microsoft Azure, and Google Cloud Platform.

As an asynchronous event-driven JavaScript runtime, Node is designed to build scalable network applications. In the following, hello readers example, many connections can be handled concurrently. Upon each connection, the callback is fired, but if there is no work to be done, Node will sleep:

const http = require('http');

const hostname = '127.0.0.1';
const port = 3001;

const server = http.createServer((request, response) => {
  response.statusCode = 200;
  response.setHeader('Content-Type', 'text/plain');
  response.end('Hello readers\n');
});

server.listen(port, hostname, () => {
  console.log(`Node.js Server running at http://${hostname}:${port}/`);
});

This is in contrast to today's more common concurrency model, where OS threads are employed. Thread-based networking is relatively inefficient and very difficult to use. Furthermore, users of Node are free from the worries of dead-locking the process, since there are no locks. Almost no function in Node directly performs I/O, so the process never blocks. Because nothing blocks, scalable systems are very reasonable to develop in Node.

Node.js is influenced by, and is similar in design to, systems such as Ruby's Event Machine ( Twisted Engine (http://twistedmatrix.com/). Node.js takes the event model a bit further, presenting an event loop (https://nodejs.org/en/docs/guides/event-loop-timers-and-nexttick/) as a runtime construct instead of a library. In other systems, there is always a blocking call to start the event loop, but that is not the case with Node.js. Typically, behavior in other systems is defined through callbacks at the beginning of a script and at the end,  the script starts a server through a blocking call such as EventMachine::run(). In Node, there is no such start the event loop call. Node simply enters the event loop after executing the input script. Node exits the event loop when there are no more callbacks to perform. This behavior is like browser JavaScript—the event loop is hidden from the user.

Node.js is designed with streaming and low latency in mind, which made HTTP a first-class citizen. This makes Node.js well suited to the foundation of a web framework or library.

Node.js is designed to run without threads, which doesn't mean you cannot take advantage of the multiple cores that are available in modern computers. You can create child processes by calling the child_process.fork() API, which will spawn multiple processes based on the CPU cores available on the machine and these child processes are designed to be easy to communicate with one another. The cluster module is built upon the same interface, which allows you to share sockets between processes to enable load balancing between your cores.

主站蜘蛛池模板: 郁南县| 新宁县| 宣威市| 鄂尔多斯市| 宜良县| 兴义市| 商水县| 北川| 镇雄县| 从化市| 辰溪县| 巩义市| 宣城市| 河北省| 儋州市| 长岭县| 南投市| 绍兴县| 榆中县| 喜德县| 宁德市| 三亚市| 湘潭县| 镇赉县| 长岛县| 临洮县| 运城市| 新化县| 泸西县| 灵武市| 泽州县| 调兵山市| 马关县| 大同市| 莫力| 阿城市| 彰武县| 化德县| 兴安盟| 廊坊市| 北辰区|