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

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.

主站蜘蛛池模板: 连城县| 万州区| 武平县| 日照市| 兴文县| 兴化市| 定陶县| 三江| 纳雍县| 连云港市| 商都县| 九龙城区| 邵阳县| 句容市| 萝北县| 绥滨县| 沿河| 威海市| 乌拉特前旗| 临猗县| 台安县| 绵竹市| 元氏县| 涟水县| 安龙县| 连山| 盈江县| 湘西| 宕昌县| 济宁市| 贵州省| 山东| 新竹县| 全椒县| 务川| 河东区| 安阳县| 乾安县| 岳普湖县| 柳河县| 石河子市|