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

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.

主站蜘蛛池模板: 龙江县| 密山市| 建阳市| 桃江县| 临沭县| 龙川县| 清苑县| 黑水县| 临猗县| 竹溪县| 中西区| 新余市| 清镇市| 克什克腾旗| 福贡县| 东乡族自治县| 榆中县| 鸡泽县| 渑池县| 隆回县| 兴和县| 虹口区| 瑞金市| 张北县| 玛纳斯县| 西林县| 万年县| 丰城市| 积石山| 时尚| 磐石市| 宣武区| 广安市| 广昌县| 博白县| 南江县| 盐城市| 崇文区| 砀山县| 迭部县| 扎兰屯市|