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

Express.js Blueprints

Book Description

Learn to use Express.js pragmatically by creating five fun and robust real-world APIs, with a bonus chapter on Koa.js

In Detail

APIs are at the core of every serious web application. Express.js is the most popular framework for building on top of Node.js, an exciting tool that is easy to use and allows you to build APIs and develop your backend in JavaScript. Express.js Blueprints consists of many well-crafted tutorials that will teach you how to build robust APIs using Express.js.

The book covers various different types of applications, each with a diverse set of challenges. You will start with the basics such as hosting static content and user authentication and work your way up to creating real-time, multiplayer online games using a combination of HTTP and Socket.IO. Next, you'll learn the principles of SOA in Node.js and see them used to build a pairing as a service. If that's not enough, we'll build a CRUD backend to post links and upvote with Koa.js!

What You Will Learn

  • Set up the Express.js framework with a basic scaffold
  • Model data using Mongoose and build a CRUD API to store the data in a database
  • Use promises to avoid callback hell in Node.js
  • Address scalability concerns and deal with race conditions
  • Apply test-driven development (TDD) by using Mocha to write tests for your apps
  • Get familiar with service-oriented architecture (SOA)
  • Integrate Socket.IO and dynamically push updates to the client
  • Write Node.js APIs by leveraging generator functions using Koa.js

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. Express.js Blueprints
    1. Table of Contents
    2. Express.js Blueprints
    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. Building a Basic Express Site
      1. Setting up Express for a static site
        1. Saying Hello, World in Express
        2. Jade templating
      2. Local user authentication
        1. User object modeling
        2. Introducing Express middleware
        3. Setting up passport
        4. Registering users
        5. Authenticating users
      3. OAuth with passport
        1. Adding OAuth to user model
        2. Getting API tokens
        3. Third-party registration and login
      4. Profile pages
        1. URL params
        2. Profile templates
      5. Testing
        1. Introducing Mocha
        2. Testing API endpoints
      6. Automate builds and deploys
        1. Introducing the Gruntfile
        2. Continuous integration with Travis
        3. Deploying Node.js applications
      7. Summary
    9. 2. A Robust Movie API
      1. Folder structure and organization
      2. Responding to CRUD operations
        1. Retrieving an actor with GET
        2. Creating a new actor with POST
        3. Updating an actor with PUT
        4. Removing an actor with DELETE
      3. Object modeling with Mongoose
      4. Generating unique IDs
      5. Validating your database
      6. Extracting functions to reusable middleware
      7. Testing
      8. Summary
    10. 3. Multiplayer Game API – Connect 4
      1. Modeling game state with Mongoose
      2. Creating a new game
        1. Input validation
        2. Getting the game state
        3. Joining a game
        4. Playing the game
        5. Testing for a tie
      3. Summary
    11. 4. MMO Word Game
      1. Gameplay
        1. Real-time application overview
        2. Keeping track of active users
      2. Schema design
        1. User schema
        2. User join
      3. Promises
        1. The then and catch method
        2. Chain multiple Promises
        3. Prevent duplicates
        4. User leaves the game
        5. Show all active users
        6. The words – Subdocuments
        7. Validate input
        8. Dealing with race conditions
        9. Test case to test race conditions
      4. Socket.IO
        1. Socket handshaking, user join
        2. Adding and pushing updates to clients
        3. Launch Socket.IO applications
        4. Test Socket.IO applications with the Socket.IO client
        5. Debug Socket.IO with Chrome Developer Tools
      5. Summary
    12. 5. Coffee with Strangers
      1. Code structure
      2. Defining routes
      3. Persisting data
        1. Exception handling
      4. Naive pairing
        1. Notes about tests
      5. Considering user history
      6. Optimizing for distance
      7. E-mail follow up
      8. Periodical tasks with node-cron
      9. Summary
    13. 6. Hacker News API on Koa.js
      1. Generator syntax
        1. Middleware philosophy
        2. Context versus req,res
          1. The link model
          2. The link routes
          3. Tying it together
      2. Validation and error handling
      3. Update route
      4. Let's perform some tests
        1. Parallel requests
        2. Rendering HTML pages
        3. Serving static assets
      5. Summary
    14. A. Connect 4 – Game Logic
    15. Index