- Hands-On Serverless Computing
- Kuldeep Chowhan
- 516字
- 2021-08-05 10:35:38
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.
- Cloud Analytics with Microsoft Azure
- 21天學(xué)通Visual Basic
- Photoshop CS3圖像處理融會貫通
- 悟透AutoCAD 2009案例自學(xué)手冊
- MCGS嵌入版組態(tài)軟件應(yīng)用教程
- 從零開始學(xué)PHP
- Photoshop行業(yè)應(yīng)用基礎(chǔ)
- INSTANT VMware vCloud Starter
- ZigBee無線通信技術(shù)應(yīng)用開發(fā)
- 手把手教你學(xué)Flash CS3
- Java組件設(shè)計
- RealFlow流體制作經(jīng)典實例解析
- 傳感技術(shù)基礎(chǔ)與技能實訓(xùn)
- 從機(jī)器學(xué)習(xí)到無人駕駛
- NetSuite ERP for Administrators