Node.Js Recipes Node.Js Recipes Is Your One-Stop Reference for Learning How to Solve Node.Js Prob- Lems

Node.Js Recipes Node.Js Recipes Is Your One-Stop Reference for Learning How to Solve Node.Js Prob- Lems

BOOKS FOR PROFESSIONALS BY PROFESSIONALS® Gackenheimer RELATED Node.js Recipes Node.js Recipes is your one-stop reference for learning how to solve Node.js prob- lems. Node.js is the de facto framework for building JavaScript-powered serv- ers. You will first be introduced to this exciting technology and what it can do, then learn through comprehensive and easy-to-follow recipes that use a problem-solution approach. Node.js Recipes teaches you to fully utilize the Node.js API, and leverage existing modules to build truly exciting projects. Because Node.js is written in JavaScript on the server, it is accessible to those who not only relish in server-side programming but also web developers who understand the ubiquitous language of the web. Node.js Recipes covers all the essential ingredi- ents required to become a seasoned Node.js developer in no time - become a Node. js pro today! Shelve in ISBN 978-1-4302-6058-5 Web Development/General User level: Intermediate 9 781430260585 SOURCE CODE ONLINE www.apress.com For your convenience Apress has placed some of the front matter material after the index. Please use the Bookmarks and Contents at a Glance links to access them. Contents at a Glance About the Author ............................................................................................................. xxiii About the Technical Reviewers ........................................................................................ xxv Acknowledgments .......................................................................................................... xxvii ■ Chapter 1: Understanding Node.js .....................................................................................1 ■ Chapter 2: Networking with Node.js ................................................................................27 ■ Chapter 3: Using the File System ....................................................................................51 ■ Chapter 4: Building a Web Server ....................................................................................81 ■ Chapter 5: Using Events and Child Processes ...............................................................109 ■ Chapter 6: Implementing Security and Cryptography ...................................................133 ■ Chapter 7: Discovering Other Node.js Modules .............................................................161 ■ Chapter 8: Creating a WebSocket Server .......................................................................191 ■ Chapter 9: Using Web Server Frameworks ....................................................................221 ■ Chapter 10: Connecting to a Data Store ........................................................................253 ■ Chapter 11: Testing in Node.js .......................................................................................281 ■ Chapter 12: Debugging and Deploying Your Application ...............................................311 Index .................................................................................................................................339 v CHAPTER 1 Understanding Node.js Node.js is a server-side framework useful for building highly scalable and fast applications. Node.js is a platform that is built on v8, the JavaScript runtime that powers the Chrome browser designed by Google. Node.js is designed to be great for intensive I/O applications utilizing the nonblocking event-driven architecture. While Node.js can serve functions in a synchronous way, it most commonly performs operations asynchronously. This means that as you develop an application, you call events with a callback registered for handling the return of the function. While awaiting the return, the next event or function in your application can be queued for execution. Once the first function completes, its callback event is executed and handled by the function call that invoked the callback. This event-driven processing is described in Node.js’s very own definition: Node.js is a platform built on Chrome’s JavaScript runtime for easily building fast, scalable network applications. Node.js uses an event-driven, non-blocking I/O model that makes it lightweight and efficient, perfect for data-intensive real-time applications that run across distributed devices. Applications written in Node.js are written in JavaScript, the ubiquitous language of the web platform. Because of the accessibility of JavaScript to many experienced developers and newcomers alike, the Node.js platform and community have taken off and have become critical parts of the development landscape for many companies and developers. This book is about Node.js. In particular this book is designed as a recipe book, which aims to provide a large set of useful and high-quality examples of what Node.js is capable of accomplishing. This book is geared for a developer who has some experience with JavaScript and at least some exposure to Node.js. By reading this book, you will gain an understanding of many of the highly utilized modules, both those native to Node.js and those written by third-party contributors, that are the main targets for Node.js developers. This first chapter is a departure from the recipe format that will follow in the rest of the book. It is broken down to get a developer up and running from scratch with installation and it gives an overview of how to function within the Node.js platform. You will get an idea of how to install Node.js and understand many of the common paradigms and the basic workflow to get a Node.js application running. As you will see, a considerable amount of time is spent covering how Node.js works. Once you have read this chapter, you should be well equipped to dive into the recipes in the chapters that follow. 1-1. Installing Node.js on Your Machine There are several ways in which an install of Node.js can happen, and they vary slightly across different operating systems. The three primary methods to install Node.js are via a binary installer, via a package manager, or by compiling the source code. To install Node.js on your machine via a binary installer, you first need the installer. Currently the only installers that are available for Node.js are for Windows and Macintosh OS X. To find these installers, you need to go to http://nodejs.org/download/. Here you will find your choice of installer to download as shown in Figure 1-1. 1 CHAPTER 1 ■ UNDERSTANDING NODE.JS Figure 1-1. Platform-specific installers available for download Windows On Windows, first download the .msi installer package. When you open the file, you will begin your walkthrough with the Setup Wizard, shown in Figure 1-2. Figure 1-2. Beginning the install 2 CHAPTER 1 ■ UNDERSTANDING NODE.JS As in most Windows applications, you will be presented with a default location to which you can install the application files. This destination, however, can be overwritten and is presented to you as in Figure 1-3. Figure 1-3. You can choose to use or overwrite the default file location The last step before finalizing your install on Windows is to set up any custom configurations that you may want for your Node.js installation. For example you could not add Node.js to your path; perhaps you want to test multiple versions and will explicitly call the executable during your testing phase. This custom step is shown in Figure 1-4. 3 CHAPTER 1 ■ UNDERSTANDING NODE.JS Figure 1-4. Custom setup OS X The installer on a Macintosh is very similar to the Windows setup. First, download the .pkg file. When you open this, it will walk you through the standard installer that runs on OS X. This presents as you see in Figure 1-5. 4 CHAPTER 1 ■ UNDERSTANDING NODE.JS Figure 1-5. Installing on OS X Sometimes when installing Node.js, you want only a subset of the potential users to be able to access it. This functionality is built into the OS X installer, presenting you with the option of how you would like Node.js installed, as shown in Figure 1-6. 5 CHAPTER 1 ■ UNDERSTANDING NODE.JS Figure 1-6. Installing for specified users Just as on Windows, you can customize the installation. Click the Custom Install button and then set your configuration accordingly as shown in Figure 1-7. For example, you may wish not to install npm, in favor of doing a more customized npm install, which we will outline in the next section. 6 CHAPTER 1 ■ UNDERSTANDING NODE.JS Figure 1-7. A custom Node.js install on OS X There are, of course, many platforms that are not Macintosh or Windows, but you would still like to not have to download and compile Node.js from sources. The solution for this is to find a package manager that will install Node. js for you. There are several package management systems that vary across platforms, each with its own style for fetching new packages. Ubuntu and Linux Mint The package for Ubuntu and Linux Mint requires that a few components be installed onto your machine before you can install Node.js. To meet these prerequisites you must first run the code shown in Listing 1-1. Listing 1-1. Ensuring Prerequisites Are Installed sudo apt-get install python-software-properties python g++ make You can then proceed with the installation by adding the repository that hosts Node.js, updating your sources, and installing with the commands shown in Listing 1-2. Listing 1-2. Installing Node.js on Ubuntu and Linux Mint sudo add-apt-repository ppa:chris-lea/node.js sudo apt-get update sudo apt-get install nodejs 7 CHAPTER 1 ■ UNDERSTANDING NODE.JS Fedora Fedora 18 has a simple Node.js install that is a

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    369 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us