- Server Side development with Node.js and Koa.js Quick Start Guide
- Olayinka Omole
- 611字
- 2021-06-10 18:57:12
A primer on Node
As JavaScript developers who may or may not have experience working with Node.js, a brief introduction to Node.js and its core ideology will help get everyone up to speed. Node.js, or simply Node, is a run-time environment that executes JavaScript outside a browser. In simpler terms, and as it relates to web developers, Node is a platform that allows developers to write JavaScript applications that can also act as servers.
JavaScript became popular for being a language used to manipulate the DOM (Document Object Model) on web pages. It was a language typically used for client-side scripting. Node, which was built on Chrome's open source v8 JavaScript engine, made it possible to run JavaScript both on the browser and the server. This was highly accepted, as developers could now develop applications with the same language on servers and web browsers.
Node is very fast and is a great choice for building HTTP applications. It processes incoming requests in a loop, called the event loop, which allows the development of fast web servers in JavaScript. Its event-driven architecture allows asynchronous operations. This means that developers can create highly scalable applications capable of processing requests asynchronously without using threading.
Asynchronous programming in Node is one of the reasons the language is so widely adopted. If you are unfamiliar with asynchronous programming or its benefits and how it compares to synchronous programming, here is a good example of a program that needs to make a request to get data from two external sources:
- In a synchronous program: The logical thing to do would be to make a request to the first external source, get a response, and then make another request to the second external source and merge the results. While this is a flow that is logical and easy to follow, it means that the wait time to service another request will be at least the sum of the wait times for each individual request. Since synchronous code leads to resource and event blocking, it does not lead an efficient solution and effectively slows down our application due to poor resource utilization.
- In an asynchronous program: Both requests can be made in parallel. When each request is completed, it notifies the main program and the results can be combined after the request that took the longest is completed. In this case, the wait time is only the time it takes for the slower request to be completed. Also, neither of the requests cause resource/event blocking, which would allow our program to respond to more new requests while waiting for results for the initial task.
Managing asynchronous actions can get quite complicated, especially in programs where the flow of logic should be synchronous. Callback functions can be used to manage asynchronous operations. Callback functions are functions that are passed to another function (the main function) to be executed inside the main function. Here's a simple example of using a callback function with the setTimeout() function:
function logTimeUp() {
console.log(“Time up!”);
}
setTimeout(logTimeUp, 1000);
The setTimeout function in JavaScript waits a given number of milliseconds and then executes the callback function passed to it. In the previous code example, we define a callback function called logTimeUp that simply prints Time up! to stdout. We then pass this function as a parameter to the setTimeout function, which will execute the callback function after 1000 milliseconds (one second). This is a classic example of how callbacks work.
In modern JavaScript, asynchronous actions can be modeled using Promises, which can be managed and consumed in multiple ways. One of these ways is using the async… await syntax.
- Objective-C應用開發全程實錄
- 高級C/C++編譯技術(典藏版)
- Flutter跨平臺開發入門與實戰
- PHP從入門到精通(第4版)(軟件開發視頻大講堂)
- C++反匯編與逆向分析技術揭秘(第2版)
- Hands-On Nuxt.js Web Development
- Apache Camel Developer's Cookbook
- 運維前線:一線運維專家的運維方法、技巧與實踐
- UML2面向對象分析與設計(第2版)
- Orchestrating Docker
- 計算機應用基礎(第二版)
- SQL Server 2012 數據庫應用教程(第3版)
- IBM RUP參考與認證指南
- Data Manipulation with R(Second Edition)
- C/C++程序設計教程