You are previewing Web Development with MongoDB and NodeJS - Second Edition.
O'Reilly logo
Web Development with MongoDB and NodeJS - Second Edition

Book Description

Build an interactive and full-featured web application from scratch using Node.js and MongoDB

About This Book

  • Configure your development environment to use Node.js and MongoDB

  • Use Node.js to connect to a MongoDB database and perform data manipulations

  • A practical guide with clear instructions to design and develop a complete web application from start to finish

  • Who This Book Is For

    This book is designed for JavaScript developers of any skill level that want to get up and running using Node.js and MongoDB to build full-featured web applications. A basic understanding of JavaScript and HTML is the only requirement for this book.

    What You Will Learn

  • Configure your development environment to use Node.js and MongoDB

  • Write and configure a web server using Node.js powered by the Express.js framework

  • Build dynamic HTML pages using the Handlebars template engine

  • Persist application data using MongoDB and Mongoose ODM

  • Test your code using automated testing tools such as the Mocha framework

  • Deploy the development environment to the cloud using services such as Heroku, Amazon Web Services, and Microsoft Azure

  • Explore Single-Page application frameworks to take your web applications to the next level

  • In Detail

    Node.js and MongoDB are quickly becoming one of the most popular tech stacks for the web. Powered by Google's V8 engine, Node.js caters to easily building fast, scalable network applications while MongoDB is the perfect fit as a scalable, high-performance, open source NoSQL database solution. Using these two technologies together, web applications can be built quickly and easily and deployed to the cloud with very little difficulty.

    The book will begin by introducing you to the groundwork needed to set up the development environment. Here, you will quickly run through the steps necessary to get the main application server up and running. Then you will see how to use Node.js to connect to a MongoDB database and perform data manipulations.

    From here on, the book will take you through integration with third-party tools for interaction with web apps. It then moves on to show you how to use controllers and view models to generate reusable code that will reduce development time. Toward the end of the book, we will cover tests to properly execute the code and some popular frameworks for developing web applications.

    By the end of the book, you will have a running web application developed with MongoDB and Node.js along with it's popular frameworks.

    Style and approach

    An easy guide to Node.js and MongoDB, which will quickly introduce you to the relevant concepts by taking you through the different steps involved in building a full-fledged web application.

    Downloading the example code for this book. You can download the example code files for all Packt books you have purchased 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 code file.

    Table of Contents

    1. Web Development with MongoDB and NodeJS Second Edition
      1. Table of Contents
      2. Web Development with MongoDB and NodeJS Second Edition
      3. Credits
      4. About the Authors
      5. About the Reviewers
      6. www.PacktPub.com
        1. Support files, eBooks, discount offers, and more
          1. Why subscribe?
          2. Free access for Packt account holders
      7. Preface
        1. What this book covers
        2. What you need for this book
        3. Who this book is for
        4. Conventions
        5. Reader feedback
        6. Customer support
          1. Downloading the example code
          2. Errata
          3. Piracy
          4. Questions
      8. 1. Welcome to JavaScript in the Full Stack
        1. A short introduction to Node.js
          1. The advantage that the V8 engine brings in
          2. Node.js is single threaded!
          3. Non-blocking asynchronous execution
        2. npm – the Node Package Manager
        3. Sharing and reusing JavaScript
          1. Not just for building web servers!
          2. Real-time web application with Socket.io
          3. Networking and file IO
          4. The origin of io.js
        4. A simple server with Node.js
          1. When to use Node.js?
        5. The NoSQL movement
        6. A short introduction to MongoDB
          1. Features of MongoDB
        7. Node and MongoDB in the wild
        8. What to expect from this book
        9. Summary
      9. 2. Getting Up and Running
        1. Installing Node.js
          1. Mac OS X
          2. Windows
          3. Linux
          4. Testing whether Node.js is installed properly
          5. Online documentation
        2. Installing MongoDB
          1. Mac OS X installation instructions
          2. Windows 7 / Windows 8 installation instructions
          3. Linux installation instructions
          4. Confirming successful MongoDB installation
          5. Bookmark the online documentation
        3. Writing your first app
          1. Creating the sample application
          2. Getting the dependent modules in place
        4. Adding the application code
          1. Understanding the code
          2. Launching the sample app
          3. Checking the actual database
        5. Summary
      10. 3. Node and MongoDB Basics
        1. A JavaScript primer
          1. Syntax basics
          2. Data types
          3. Understanding the scope of variables
          4. Operators and flows
          5. Understanding objects
          6. Understanding arrays
          7. Understanding functions
          8. Anonymous functions and callbacks
          9. JSON
        2. The basics of Node.js
          1. Event-driven
          2. Asynchronous execution
          3. The module system
          4. The Node.js core
        3. Installing modules using npm
        4. The basics of MongoDB
          1. The Mongo shell
          2. Inserting data
          3. Querying
          4. Updating data
          5. Deleting data
        5. Additional resources
        6. Summary
      11. 4. Introducing Express
        1. Web application frameworks
        2. What is Express.js?
        3. Building a complete web application
          1. Designing the web application
          2. Organizing the files
          3. Creating the application's entry point
          4. Booting up the application
        4. Using and understanding middleware
        5. The configure module
          1. Activating the configure module
        6. Routers and controllers
        7. Custom middleware
        8. Handlebars as view engines
        9. Summary
      12. 5. Templating with Handlebars
        1. Templating engines
        2. Server-side and client-side templating
          1. Client-side templating
          2. Server-side templating
        3. Views
        4. Layouts
        5. Partial views
        6. The basics of Handlebars
          1. Binding an object to the template
          2. Embedding presentation logic
        7. Handlebars helpers
          1. Global helpers
          2. View-specific helpers
        8. Rendering the views
        9. Summary
      13. 6. Controllers and View Models
        1. Controllers
        2. View models
        3. Updating the home controller
        4. Updating the image controller
          1. Displaying an image
          2. Uploading an image
        5. Helpers for reusable code
          1. The sidebar module
          2. The stats module
          3. The images module
          4. The comments module
          5. Testing the sidebar implementation
        6. Iterating on the UI
        7. Summary
      14. 7. Persisting Data with MongoDB
        1. Using MongoDB with Node.js
          1. Connecting to MongoDB
          2. Inserting a document
          3. Retrieving a document
        2. Introducing Mongoose
          1. Schemas
          2. Models
          3. Built-in validation
          4. Static methods
          5. Virtual properties
          6. Connecting with Mongoose
        3. Defining the schemas and models
          1. The models index file
        4. Adding CRUD to the controllers
          1. The home controller
          2. The image controller
            1. Index – retrieving an image model
            2. Create – inserting an image model
            3. Testing everything out so far
            4. The Like button and updating an image model
            5. Comment – inserting a comment model
            6. Wrapping it up
          3. Helpers
            1. Introducing the async module
            2. The comments helper
            3. The sidebar helper
            4. Troubleshooting
            5. The stats helper
            6. The popular images helper
        5. Iterating by adding an image removal capability
          1. Adding a route
          2. Adding a controller handler
          3. Updating the Handlebars image page template
          4. Updating jQuery
        6. Refactoring and improvements
        7. Summary
      15. 8. Creating a RESTful API
        1. What is an API?
        2. What is a RESTful API?
        3. Introducing Postman REST Client
          1. Installation instructions
          2. A quick tour of Postman REST Client
          3. Using the JSONView Chrome extension
        4. Creating a basic API server
          1. Creating sample JSON data
        5. Responding to GET requests
        6. Receiving data – POST and PUT requests
        7. Removing data – DELETE
        8. Consuming external APIs from Node.js
          1. Consuming an API endpoint using request
        9. Summary
      16. 9. Testing Your Code
        1. The tools of the trade
          1. Running tests with the Mocha framework
          2. Asserting tests with Chai.js
          3. Getting started with Chai
          4. Spies and stubs with Sinon.js
          5. Stubbing node modules with Proxyquire
        2. Writing and running your first test
          1. Writing a test helper
        3. Testing the application
          1. Testing the routes
          2. Testing the server
          3. Testing a model
          4. Testing a controller
        4. Spy and stub everything!
        5. Summary
      17. 10. Deploying with Cloud-Based Services
        1. Cloud versus traditional hosting
          1. Infrastructure as a Service versus Platform as a Service
        2. An introduction to Git
        3. Deploying your application
          1. Nodejitsu
          2. Heroku
          3. Amazon Web Services
            1. Create a MongoLab account and database
            2. Create and configure the AWS environment
          4. Microsoft Azure
          5. Digital Ocean
        4. Summary
      18. 11. Single-Page Applications with Popular Frontend Frameworks
        1. What is a Single-Page Application?
        2. Why use a frontend framework?
          1. The TodoMVC project
          2. Backbone.js
          3. Ember.js
          4. AngularJS
        3. Frontend development tools
          1. Automated build task managers
          2. Dependency management
          3. Modularity
          4. HTML template-rendering engines
          5. CSS transpiling
        4. Testing and test-driven development
          1. PhantomJS headless browser
        5. Summary
      19. 12. Popular Node.js Web Frameworks
        1. Koa
        2. Meteor
        3. Sails
        4. Hapi
        5. Flatiron
        6. Summary
      20. Index