ebook img

Basarat Ali Syed (auth.)-Beginning Node.js-Apress (2014) PDF

297 Pages·2014·6.94 MB·English
by  
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview Basarat Ali Syed (auth.)-Beginning Node.js-Apress (2014)

BOOKS FOR PROFESSIONALS BY PROFESSIONALS® Syed Beginning Node.js RELATED 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. Using this book you will learn important Node.js concepts for server-side programming. You will begin with an easy-to-follow pure JavaScript primer, which you can skip if you’re confident of your JS skills. You’ll then delve into Node.js concepts such as streams and events, and the technology involved in building full- stack Node.js applications. You’ll also learn how to test your Node.js code, and deploy your Node.js applications on the internet. 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. Shelve in ISBN 978-1-4842-0188-6 Web Development/JavaScript 54999 User level: Beginning SOURCE CODE ONLINE 9781484201886 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 ��������������������������������������������������������������������������������������������������������������xvii About the Technical Reviewer �������������������������������������������������������������������������������������������xix Acknowledgments �������������������������������������������������������������������������������������������������������������xxi Introduction ���������������������������������������������������������������������������������������������������������������������xxiii ■ Chapter 1: Setting Up for Node�js Development ����������������������������������������������������������������1 ■ Chapter 2: Understanding Node�js �����������������������������������������������������������������������������������17 ■ Chapter 3: Core Node�js ���������������������������������������������������������������������������������������������������41 ■ Chapter 4: Node�js Packages �������������������������������������������������������������������������������������������65 ■ Chapter 5: Events and Streams ���������������������������������������������������������������������������������������91 ■ Chapter 6: Getting Started with HTTP ����������������������������������������������������������������������������115 ■ Chapter 7: Introducing Express �������������������������������������������������������������������������������������141 ■ Chapter 8: Persisting Data ���������������������������������������������������������������������������������������������165 ■ Chapter 9: Front-End Basics ������������������������������������������������������������������������������������������181 ■ Chapter 10: Simplifying Callbacks ��������������������������������������������������������������������������������197 ■ Chapter 11: Debugging ��������������������������������������������������������������������������������������������������225 ■ Chapter 12: Testing ��������������������������������������������������������������������������������������������������������241 ■ Chapter 13: Deployment and Scalability �����������������������������������������������������������������������255 Index ���������������������������������������������������������������������������������������������������������������������������������275 v Introduction Undoubtedly, personal computing has revolutionized the way we live and work today. The Web has further revolutionized the way we use applications. When it was first introduced, the Internet was designed to present information in the form of documents. Later, JavaScript was added, which has been the key ingredient for the innovation we see on the Web today. Web applications are platform-independent, seamlessly updating, safe by default, and available anytime and everywhere. No wonder it is difficult to get started in a developer role today without some knowledge of how the Web works. Because of the importance of the Web and the pivotal role that JavaScript plays in web development, you can find a solution for most technical problems in some open source JavaScript project. Node.js allows you to use all these innovative JavaScript projects on the server the same as on the client browser. Using JavaScript on the server also reduces the context switching that needs to happen in your brain as you change programming language and associated code conventions. This is the emotional side of why you should use Node.js. This book is designed to be a gentle introduction to Node.js as well as JavaScript. No prior software development experience is assumed beyond a basic programming course. Since we clearly present the technical reasons behind the creation of Node.js, this book is also great if you are already comfortable programming in another environment, such as C# or Java, and are curious what all the fuss around Node.js is about. This book covers all the main areas of Node.js software development from setup to deployment so when you finish this book, you should be able to start using Node. js immediately and be ready to share your projects with the world. xxiii Chapter 1 Setting Up for Node.js Development In this chapter, we discuss how to set up a Node.js development environment as we guide you through the installation process of Node.js on various platforms. Then we give you a tour of the Node.js REPL (read-evaluate-print-loop) and show you how you can run Node.js applications. Finally, we provide examples of Integrated Development Environments (IDEs) that can help you deliver applications faster and make your journey more enjoyable. Installing Node.js You no longer need to build Node.js from source in order to develop Node.js applications. Node.js now provides installers for Windows as well as Mac OS X, and it can be installed in the same way as any other application on these platforms (Figure 1-1). You can download Node.js installers from http://nodejs.org/download/. Figure 1-1. Node.js download page listing installers 1 Chapter 1 ■ Setting Up for node.jS development In the next section, we will guide you through the important steps for your operating system (OS). You can safely skip the section not relevant to your current OS. Installing on Windows The website for Node.js lists “Windows Binary (.exe)” and “Windows Installer (.msi).” You do not want to use the windows binary (.exe) for development, as it does not contain important things such as Node Package Manager (NPM), which we cover in Chapter 4. Node.js provides separate installers (.msi) for 32-bit and 64-bit Windows. We recommend that you install based on your platform. You launch the installer as you would any other installer on Windows (Figure 1-2). Figure 1-2. Node.js Setup Wizard on Windows We recommend that you install to the default directory and with the default options when starting out for the first time. It is especially important that you let the installer Add to PATH (Figure 1-3). 2 Chapter 1 ■ Setting Up for node.jS development Figure 1-3. Default options for Node.js installer on Windows After installation, uninstalling and reinstalling Node.js are extremely easy. If you run the installer again, you will be prompted with the Remove option, as shown in Figure 1-4. Figure 1-4. Node.js uninstaller for Windows Since the installer set up the system PATH, you can run Node.js from the command prompt (search for “command prompt” in the Windows start menu). We can start up Node.js by simply typing node in cmd (Figure 1-5). This puts you in the REPL, which we explain in the next section. 3 Chapter 1 ■ Setting Up for node.jS development Figure 1-5. Running Node.js from the command line Installing on Mac OS X Download the Mac OS X installer provided by the Node.js team from http://nodejs.org/download/. The installer is a .pkg file you can launch from Finder (Figure 1-6). Figure 1-6. Node.js Installer for Mac OS X When starting out, stick with the defaults and install for all users (Figure 1-7). 4 Chapter 1 ■ Setting Up for node.jS development Figure 1-7. Node.js setup for all users option Once complete, the installer will inform you of the two binaries it installed (node and npm), as shown in Figure 1-8. Figure 1-8. Node.js binaries installed We will cover npm extensively in Chapter 4. Your main executable for running JavaScript in Node.js is node (Figure 1-9). For Mac OS X, you can start node from Terminal (use Mac OS X spotlight to search for Terminal). If you execute node in Terminal, it will start the Node.js REPL, which we discuss next. 5 Chapter 1 ■ Setting Up for node.jS development Figure 1-9. Running Node.js from the command line on Mac OS X Using the REPL Node.js provides you with a REPL (read-evaluate-print-loop) so that you can test arbitrary JavaScript and experiment and explore solutions to the problem you are trying to solve. When you run node without any command line arguments, it puts you in the REPL. To view the options available to you, as shown in Figure 1-10, type .help and press Enter. Figure 1-10. Node.js REPL help You can execute arbitrary JavaScript in the REPL and see its result immediately, as shown in Figure 1-11. Figure 1-11. Executing JavaScript in the Node.js REPL At each step, the REPL prints the outcome of the last statement executed. The REPL does not execute your input code until all brackets have been balanced. To execute multiple lines, simply wrap them in parentheses. The REPL uses (…) to denote that it is waiting for the complete code before executing. Simply close the parentheses and press Enter for the REPL to evaluate the entered JavaScript (see Figure 1-12). To exit from inside a block (...) without executing what you have already entered, simply type .break or press Ctrl+C. 6

Description:
For your convenience Apress has placed some of the front .. Node.js has seen fantastic community support since its beginning. Thanks to the
See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.