Book Image

Mastering JavaScript Single Page Application Development

Book Image

Mastering JavaScript Single Page Application Development

Overview of this book

Single-page web applications—or SPAs, as they are commonly referred to—are quickly becoming the de facto standard for web app development. The fact that a major part of the app runs inside a single web page makes it very interesting and appealing. Also, the accelerated growth of browser capabilities is pushing us closer to the day when all apps will run entirely in the browser. This book will take your JavaScript development skills to the next level by teaching you to create a single-page application within a full-stack JavaScript environment. Using only JavaScript, you can go from being a front-end developer to a full-stack application developer with relative ease. You will learn to cross the boundary from front-end development to server-side development through the use of JavaScript on both ends. Use your existing knowledge of JavaScript by learning to manage a JSON document data store with MongoDB, writing a JavaScript powered REST API with Node.js and Express, and designing a front-end powered by AngularJS. This book will teach you to leverage the MEAN stack to do everything from document database design, routing REST web API requests, data-binding within views, and adding authentication and security to building a full-fledged, complex, single-page web application. In addition to building a full-stack JavaScript app, you will learn to test it with JavaScript-powered testing tools such as Mocha, Karma, and Jasmine. Finally, you will learn about deployment and scaling so that you can launch your own apps into the real world.
Table of Contents (20 chapters)
Mastering JavaScript Single Page Application Development
Credits
About the Authors
About the Reviewer
www.PacktPub.com
Preface
Free Chapter
1
Getting Organized with NPM, Bower, and Grunt
13
Testing with Mocha, Karma, and More

Preface

Created in 1995 by Brendan Eich of Netscape, JavaScript has gone from being a toy scripting language used only in browsers to being one of the most popular languages in the world for full-stack development. With the introduction of Node.js, built on top of Chrome’s V8 JavaScript Engine, developers are now able to build powerful and performant applications using JavaScript. With the addition of MongoDB, a modern NoSQL database, applications can utilize JavaScript at every tier.

Many popular web applications are implemented, partially or in whole, as single-page applications, or SPAs. With SPAs, users load a single web page, and that page is updated dynamically in response to user interaction or incoming server data. The advantage is an application experience that is smoother, replicates native application interaction, and may require less network traffic and server load.

The MEAN stack is a set of JavaScript tools representing a full stack, from database to runtime environment, application framework, and frontend. This book provides a thorough background in building SPAs using the MEAN stack and other tools in the JavaScript ecosystem. It covers the basics of SPA architecture and JavaScript tools. The book then expands to more advanced topics, such as building, securing, and deploying an SPA built with the MEAN stack.

What this book covers

Chapter 1, Getting Organized NPM, Bower, and Grunt, introduces JavaScript frontend package-management, build, and task-running tools. These tools form the foundation for you to set up an ideal development environment.

Chapter 2, Model-View-Whatever, goes beyond the original MVC design pattern and explores its translation into frontend frameworks and its evolution into Model-View-*, or Model-View-Whatever (MVW), in which the Controller layer is more open ended and often abstracted into other layers more suited to an SPA environment.

Chapter 3, SPA Essentials - Creating the Ideal Application Environment, introduces you to the common components/layers of an SPA, the best practices and variations around these components, and how to put them together and lay the groundwork for a modern SPA.

Chapter 4, REST is Best - Interacting with the Server Side of Your Application, goes into further detail about the server side of SPA architecture—in particular, with regard to the REST (representational state transfer) architectural pattern. You’ll become familiar with different methods to interact with a REST API using JavaScript and AJAX, with practical examples.

Chapter 5, Its All About the View, focuses on the concept of views in the SPA architecture and how views can be initialized in a single-page container. It discusses JavaScript templates and provides examples from different libraries, going deeper into how AngularJS views are implemented.

Chapter 6, Data Binding, and Why You Should Embrace It, teaches you about data binding, describes one-way versus two-way data binding, and discusses the practicality of data binding in an SPA and why you should use it. You will also cover the continual evolution of the ECMAScript/JavaScript standard and how it now supports native data binding in some clients.

Chapter 7, Leveraging the MEAN Stack, introduces you to the MEAN stack (MongoDB, Express, AngularJS, and Node.js) and how they work together. You will install and configure MongoDB and Node.js and explore working with each on the command line. You’ll create a database for a new SPA and learn about AngularJS and Express, the two other pieces of the stack.

Chapter 8, Manage Data Using MongoDB, teaches you how to create and manage databases in MongoDB. Using the command line, you will execute CRUD (create, read, update, and delete) functions.

Chapter 9, Handling Web Requests with Express, familiarizes you with the Express routing middleware and handling requests from and responses to the browser. After configuring the Express router, you’ll create a number of routes, which return dynamically generated data to the web browser when requested, organize your routes logically, and handle POST requests from forms.

Chapter 10, Displaying Views, explores combining dynamic view rendering in Express along with AngularJS. You will configure the Express application to use EJS (Embedded JavaScript) templates and use Bootstrap for basic styling.

Chapter 11, Adding Security and Authentication, teaches you how to secure the Express-based SPA by preventing common exploits such as cross-site request forgery (CSRF). You will install passport-authentication middleware for Node.js and configure it for local authentication and set up session management.

Chapter 12, Connecting the App to Social Media, extends the SPA by connecting it to multiple social media platforms. You will set up passport authentication using Facebook and Twitter strategies and share data from the SPA.

Chapter 13, Testing with Mocha, Karma, and More, teaches you testing, both on the server side and in the view.

Chapter 14, Deploying and Scaling the SPA, walks you through setting up a production database on MongoLab and deploying your SPA to Heroku. Finally, you will explore scaling your SPA in the cloud.

What you need for this book

The book requires very few pieces of software besides your web browser and your operating system's command line or terminal. You will need an editor for writing code. Any text editor will do, from Notepad to an IDE such as Jetbrains WebStorm.

Who this book is for

This book is ideal for JavaScript developers who want to build complex SPAs in JavaScript. Some basic understanding of SPA concepts will be helpful but are not essential.

Conventions

In this book, you will find a number of text styles that distinguish between different kinds of information. Here are some examples of these styles and an explanation of their meaning.

Code words in text, database table names, folder names, filenames, file extensions, path names, dummy URLs, user input, and Twitter handles are shown as follows: "We can include other contexts through the use of the include directive."

A block of code is set as follows:

module.exports = function(grunt) {

    grunt.initConfig({
        pkg: grunt.file.readJSON('package.json'),

When we wish to draw your attention to a particular part of a code block, the relevant lines or items are set in bold:

    grunt.initConfig({
        pkg: grunt.file.readJSON('package.json'),
        clean: ['dist/**'],
        copy: {
            main: {
        files: [

Any command-line input or output is written as follows:

$ npm install -g grunt-cli
[email protected] /usr/local/lib/node_modules/grunt-cli
├── [email protected]
├── [email protected] ([email protected])
└── [email protected] ([email protected], [email protected])

New terms and important words are shown in bold. Words that you see on the screen, for example, in menus or dialog boxes, appear in the text like this: "Clicking the Next button moves you to the next screen."

Note

Warnings or important notes appear in a box like this.

Tip

Tips and tricks appear like this.

Reader feedback

Feedback from our readers is always welcome. Let us know what you think about this book-what you liked or disliked. Reader feedback is important for us as it helps us develop titles that you will really get the most out of. To send us general feedback, simply e-mail [email protected], and mention the book's title in the subject of your message. If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide at www.packtpub.com/authors.

Customer support

Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.

Downloading the example code

You can download the example code files for this book from your account at http://www.packtpub.com. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.

You can download the code files by following these steps:

  1. Log in or register to our website using your e-mail address and password.

  2. Hover the mouse pointer on the SUPPORT tab at the top.

  3. Click on Code Downloads & Errata.

  4. Enter the name of the book in the Search box.

  5. Select the book for which you're looking to download the code files.

  6. Choose from the drop-down menu where you purchased this book from.

  7. Click on Code Download.

Once the file is downloaded, please make sure that you unzip or extract the folder using the latest version of:

  • WinRAR / 7-Zip for Windows

  • Zipeg / iZip / UnRarX for Mac

  • 7-Zip / PeaZip for Linux

The code bundle for the book is also hosted on GitHub at https://github.com/PacktPublishing/Mastering-JavaScript-Single-Page-Application-Development. We also have other code bundles from our rich catalog of books and videos available at https://github.com/PacktPublishing/. Check them out!

Errata

Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books-maybe a mistake in the text or the code-we would be grateful if you could report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting http://www.packtpub.com/submit-errata, selecting your book, clicking on the Errata Submission Form link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded to our website or added to any list of existing errata under the Errata section of that title.

To view the previously submitted errata, go to https://www.packtpub.com/books/content/support and enter the name of the book in the search field. The required information will appear under the Errata section.

Piracy

Piracy of copyrighted material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works in any form on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy.

Please contact us at [email protected] with a link to the suspected pirated material.

We appreciate your help in protecting our authors and our ability to bring you valuable content.

Questions

If you have a problem with any aspect of this book, you can contact us at [email protected], and we will do our best to address the problem.