Node.js and REST
There are currently too many technologies out there—be it programming languages, platforms, or frameworks. Why is it, then, that Node.js—a project that’s hasn’t even reached version 1.0 at the time of this writing—is so popular these days?
- PDF / 474,332 Bytes
- 29 Pages / 504 x 720 pts Page_size
- 15 Downloads / 185 Views
Node.js and REST There are currently too many technologies out there—be it programming languages, platforms, or frameworks. Why is it, then, that Node.js—a project that’s hasn’t even reached version 1.0 at the time of this writing—is so popular these days? Advances in hardware make it possible for developers to focus less on hyper- optimizing their code to gain speed, allowing them to focus more on speed of development; thus, a new set of tools has surfaced. These tools make it easier for novice developers to develop new projects, while at the same time provide advanced developers with access to the same type of power they got with the old tools. These tools are the new programming languages and frameworks of today (Ruby on Rails, Laravel, Symfony, Express.js, Node.js, Django, and much more). In this chapter, I’ll go over one of the newest of these: Node.js. It was created in 2009 by Ryan Dahl and sponsored by Joyent, the company for which Dahl worked. At its core, Node.js1 utilizes the Google V82 engine to execute JavaScript code on the server side. I’ll cover its main features to help you understand why it is such a great tool for API development. The following are some of the aspects of Node.js covered in this chapter: •
Async programming: This is a great feature of Node.js. I’ll discuss how you can leverage it to gain better results than if using other technologies.
•
Async I/O: Although related to async programming, this deserves a separate mention because in input/output-heavy applications, this particular feature presents the winning card for choosing Node.js over other technologies.
See http://en.wikipedia.org/wiki/Node.js. See http://en.wikipedia.org/wiki/V8_(JavaScript_engine).
1 2
© Fernando Doglio 2018 F. Doglio, REST API Development with Node.js, https://doi.org/10.1007/978-1-4842-3715-1_3
71
Chapter 3
Node.js and REST
•
Simplicity: Node.js makes getting started and writing your first web server very easy. You’ll see some examples.
•
Amazing integration with JSON-based services (like other APIs, MongoDB, etc.).
•
The community and the Node package manager (npm): I’ll go over the benefits of having a huge community of developers using the technology and how npm has helped.
•
Who’s using it?: Finally, I’ll quickly go over some of the big companies using Node.js in their production platforms.
Asynchronous Programming Asynchronous (or async) programming is perhaps at the same time one of the best and most confusing features of Node.js. Asynchronous programming means that for every asynchronous function that you execute, you can’t expect it to return the results before moving forward with the program’s flow. Instead, you’ll need to provide a callback block/function that will be executed once the asynchronous code finishes. Figure 3-1 shows a regular, non-asynchronous flow.
72
Chapter 3
Node.js and REST
Figure 3-1. A synchronous execution flow Figure 3-1 represents a set of instructions that run in a synchronous manner. To execute Instruction #4, you need to wait as long as
Data Loading...