O'Reilly logo

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Web Development with MongoDB and Node.js

Book Description

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

In Detail

Node.js and MongoDB are quickly becoming the most popular technologies in the world of full stack JavaScript development. Powered by Google's V8 JavaScript runtime, 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.

This book will enable you to leverage the key features of both MongoDB and Node.js in web and mobile app development. Starting with setting up your development environment, it will guide you through how to write your first Node.js web server application from scratch. Along the way, you will learn about best practices and common mistakes while mastering the core concepts of web development.

What You Will Learn

  • Set up a development environment and install 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
  • Create and consume RESTful APIs
  • Test your code using automated testing tools
  • Deploy 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

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 files e-mailed directly to you.

Table of Contents

  1. Web Development with MongoDB and Node.js
    1. Table of Contents
    2. Web Development with MongoDB and Node.js
    3. Credits
    4. About the Author
    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. Node.js changed JavaScript forever
        1. Asynchronous callbacks
        2. Node Package Manager
        3. Networking and file IO
        4. Not just on the web
        5. Real-time web with Socket.io
      2. The NoSQL movement
        1. Node and MongoDB in the wild
        2. What to expect from this book
      3. Summary
    9. 2. Getting Up and Running
      1. Environment assumptions and requirements
      2. Installing Node.js
        1. Mac OS X installation instructions
        2. Windows 7 or 8 installation instructions
        3. Linux installation instructions
        4. Confirming successful Node.js installation
        5. Bookmarking the online documentation
      3. Installing the MongoDB server
        1. Mac OS X installation instructions
        2. Windows 7 or 8 installation instructions
        3. Linux installation instructions
        4. Confirming successful MongoDB installation
        5. Bookmarking the online documentation
      4. Writing your first app
        1. The code
        2. Launch the sample app
        3. Check the actual database
      5. Summary
    10. 3. Node and MongoDB Basics
      1. A JavaScript Primer
        1. Declaring variables
        2. Declaring functions
        3. Declaring objects
        4. Functions are objects
        5. Anonymous functions and callbacks
        6. Arrays
        7. Conditions and comparison operators
        8. Flow
        9. JSON
      2. The basics of NodeJS
        1. Event driven
        2. Asynchronous
        3. Require and modules
        4. The NodeJS 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. Writing an Express.js Server
      1. What is Express.js?
      2. Building a complete web application
      3. Organizing the files
      4. Server.js – where it all begins
        1. Booting up server.js
      5. Configuration module
        1. Handlebars view engine
          1. Other template engines
        2. Using and understanding middleware
        3. Introducing Connect
        4. Activating the configure module
      6. Routers and controllers
      7. Custom middleware
      8. Migrating to Express v4.0.0
        1. Using new middleware
          1. server/configure.js
          2. server/routes.js
      9. Summary
    12. 5. Dynamic HTML with Handlebars
      1. Basic syntax for Handlebars
      3. Layouts
      4. Partial views
      5. Handlebars Helpers
        1. Global helpers
        2. View-specific helpers
      6. Rendering the views
      7. 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
        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 schema and models
        1. 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
            1. Testing everything out so far
          3. Like – updating an image model
          4. Comment – inserting a comment model
          5. Wrapping it up
        3. Helpers
          1. Introducing the async module
          2. The comments helper
          3. The helper sidebar
          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 the 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. Tools of the trade
        1. Running tests with the Mocha framework
        2. Asserting tests with Chai.js
          1. Installing Chai.js as a devDependency
        3. Spies and stubs with Sinon.js
        4. 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 (IaaS) versus Platform as a Service (PaaS)
      2. Introduction to Git
      3. Deploying your application
        1. Nodejitsu
        2. Heroku
        3. Amazon Web Services (AWS)
          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. Meteor
      2. Sails
      3. hapi
      4. Koa
      5. Flatiron
      6. Summary
    20. Index