©  Caio Ribeiro Pereira 2016

Caio Ribeiro Pereira, Building APIs with Node.js , 10.1007/978-1-4842-2442-7_1

1. Introduction to Node.js

Caio Ribeiro Pereira

(1)São Vicente - SP, São Paulo, Brazil

Electronic supplementary material

The online version of this chapter (doi:10.​1007/​978-1-4842-2442-7_​1) contains supplementary material, which is available to authorized users.

What Is Node.js?

Node.js is a low-level, highly scalable platform. It was created explicitly to be an experiment in asynchronous processing. Using Node.js, you will program directly with many network and Internet protocols or use libraries that have access to operating system (OS) resources. To program in Node.js you only need to master JavaScript language; that’s right, only JavaScript! The JavaScript runtime used in this platform is the famous Javascript V8, which is also used in Google Chrome.

Main Features

Single-Thread Architecture

A single-thread architecture works with only the main thread instance for each initiated process. If you are used to programming in multithread architectures, like Java or .NET to use the concurrency processing, unfortunately that won’t be possible with Node.js. However, you can work with parallel processing using multiple processes.

For example, you can use a native library from Node.js called clustersthat allows you to implement a network of multiple processes. In Node.js you can create N-1 processes to work with parallel processing, as the main process (or master process) works on balancing the load among the other processes (or slave process). If your server has multiple cores, applying this technique will optimize the usage of the processing.

Another way is to use asynchronous programming, which is one of the main resources from JavaScript . The asynchronous functions on Node.js work as nonblocking I/O: If your application has to read a huge file it will not block the CPU, because the I/O operation runs out of a thread pool, allowing your application to continue to process other tasks requested by other users.

Event-Loop

Node.js has an event-oriented paradigm. It follows the same event-oriented philosophy on the JavaScript client side; the only difference is the type of event, meaning that there are not events such as mouse’s click, key up from the keyboard, or any other events from web browsers. In fact, Node.js works with I/O events; for example, connect from a database, open a file or read data from a stream, and many others.

The event-loop (Figure 1-1) is the agent responsible for noticing and emitting events. Simply speaking, it is an infinite loop that in each iteration verifies in the event queue if some event was triggered. When an event is triggered, the event-loop executes it and sends it to the queue of executed events. When an event is running, we can write any business logic on it inside the callback function commonly used in JavaScript .

A435096_1_En_1_Fig1_HTML.jpg
Figure 1-1. The event-loop process

Why Do I Need to Learn Node.js?

  • JavaScripteverywhere: Node.js uses JavaScript as a server-side programming language. This feature allows you to streamline your learning process; after all, the language is the same as the JavaScript client side. Your main challenge in this platform is to learn how to work with the asynchronous programming to make the most of this technique. Another advantage of working with JavaScript is that you can keep a project that is easy to maintain, as long as you know how to write codes using JavaScript. You are going to easily find professionals for your projects and you’re going to spend less time studying a new server-side language. A technical JavaScript advantage in comparison with other back-end languages is that you will no longer use specific frameworks to parse JavaScript Object Notation (JSON) objects; after all, the JSON from the client side will be same as on the server side. There are also cases of applications running databases that persist JSON objects, such as the popular NoSQL: MongoDB, CouchDB, and Riak. An important detail is that Node.js uses many functionalities from ECMAScript 6 , allowing you write elegant and robust JavaScript code.

  • Active community: This is one of the biggest advantages in Node.js. Currently, there are many communities around the world striving to make Node.js a huge platform, either writing posts and tutorials, giving speeches at events, or publishing and maintaining new modules.

  • Active open source modules: Today there are more than 300,000 open source modules published into NPM that can be used instantly for Node.js projects.

  • Ready to real time: Node.js has become popular thanks to some frameworks for real-time communications between client and server. SockJS and Socket.IO are good examples. They are compatible with the WebSockets protocol and they allow you to traffic data through a single bidirectional connection, treating all data by JavaScript events.

  • Big players: LinkedIn, WalMart, Groupon, Microsoft, Netflix, Uber, and Paypal are some of the big companies using Node.js, and there are many more.

Conclusion

Up to this point, I explained the main concepts and advantages of using Node.js. In the next chapters, we are going to see how it works in practice, with a single condition: Open your mind to new ideas and read this book with excitement

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset