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

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.

主站蜘蛛池模板: 射阳县| 常宁市| 蒙阴县| 达日县| 阿拉尔市| 道真| 昭觉县| 兖州市| 海门市| 吴桥县| 呼伦贝尔市| 凭祥市| 营山县| 涡阳县| 郴州市| 瓦房店市| 荔浦县| 沭阳县| 瓮安县| 景德镇市| 洛浦县| 宁陵县| 淮阳县| 文化| 淅川县| 阳曲县| 嫩江县| 浦城县| 全州县| 江川县| 阿勒泰市| 绥化市| 左云县| 定边县| 兴和县| 杂多县| 德庆县| 韶山市| 化德县| 马公市| 祁阳县|