English | 2014 | ISBN: 978-1-484201-88-6 | 297 Pages | PDF | 10 MB
Beginning Node.js is your step-by-step guide to learning all the aspects of creating maintainable Node.js applications. You will see how Node.js is focused on creating high-performing, highly-scalable websites, and how easy it is to get started. Many front-end devs regularly work with HTML, CSS, PHP, even WordPress, but haven’t yet got started with Node.js. This book explains everything for you from a beginner level, enabling you to start using Node.js in your projects right away.
Node.js is a great and simple platform to work with. It is lightweight, easy to deploy and manage. You will see how using Node.js can be a fun and rewarding experience – start today with Beginning Node.js.
In this chapter, we discussed a few important maintainability topics that you should be aware of in order to become a successful Node.js developer. We looked closely at require/module.exports combinations, giving you a firm understanding of the principles of Node.js modules and its simplicity. Then we discussed a few core built-in Node.js modules. (We will take a look at more of these core modules as we learn about events, streams, and specific areas such as TCP/HTTP.) Finally, we discussed the differences between AMD and CommonJS and how to reuse Node.js code in the browser.
In the next chapter, we will discuss one of the great things about Node.js—its open source ecosystem. There are packages upon packages of open source Node.js projects available, and we will show you how you can take advantage of them using NPM.
Events and Streams
The most important concepts are that of Readable streams, Writable streams, Duplex streams, and Transform streams. A readable stream is one that you can read data from but not write to. A good example of this is process. stdin, which can be used to stream data from the standard input. A writable stream is one that you can write to but not read from. A good example is process.stdout, which can be used to stream data to the standard output. A duplex stream is one that you can both read from and write to. A good example of this is the network socket. You can write data to the network socket as well as read data from it. A transform stream is a special case of a duplex stream where the output of the stream is in some way computed from the input. These are also called through streams. A good example of these is encryption and compression streams.
All of the basic building blocks of streams are present in the Node.js core stream module that you load using require(‘stream’) . There are base classes for implementing streams present in this module, aptly called Readable, Writable, Duplex, and Transform.
Streams in Node.js are based on events, which is why it was important to have a firm understanding of events before we could dive into streams. All of these stream classes inherit from a base abstract Stream class (abstract because you should not use it directly), which in turn inherits from EventEmitter (which we saw earlier). This hierarchy is demonstrated in Listing 5-35.
Getting Started with HTTP
We started this chapter with a thorough examination of the built-in HTTP functionality found in core Node.js. This close look was necessary because the community frameworks depend on it to provide advanced features, and having a good grasp of the basics will go a long way in making you a successful Node.js developer.
We followed this with a deep dive into the connect middleware framework. Connect allows you to share and create manageable HTTP plug-ins. This helps you manage complexity, which increases your productivity as you can divide and conquer software requirements. Connect is focused on HTTP applications and not very focused on web sites, which is something that ExpressJS (another framework from the connect team) is well-suited for. We will look at ExpressJS later in this book. But know that all of connect middleware is usable with ExpressJS since it has the same middleware convention (that is, the use member function).
Although we wrote about a few middlewares in this chapter, there are a ton of great (tested and secure) middlewares already written by the Node.js community. We will examine those in the next chapter. Finally, we demonstrated how simple HTTPS is in Node.js. Whenever possible, you should just use HTTPS for all your servers.
Node.js is focused on Application servers. That means you can plug in deeply into HTTP and fully embrace all the innovation that the Web has to offer. In fact, it is not uncommon to find network protocol developers moving to Node. js for prototyping because of its low level access, yet great memory management.