You are previewing Getting Started with hapi.js.
O'Reilly logo
Getting Started with hapi.js

Book Description

Build well-structured, testable applications and APIs using hapi.js

About This Book

  • With the help of this book, you will improve your productivity as a developer and that of your team by focusing on business logic utilizing the structure that Hapi.js provides

  • You will be introduced to a real-world problem and we’ll demonstrate how to use the tools Hapi provides to resolve it

  • This is the only book with a learn-by-example approach

  • Who This Book Is For

    If you are a JavaScript developer with or without Node.js experience and would like to learn to build applications, APIs, and web servers with the best-in-class framework hapi.js, this book is perfect for you.

    What You Will Learn

  • Increase your productivity by taking advantage of the out-of-the-box features hapi.js provides

  • Build secure API servers

  • Create websites and applications using your favorite templating language

  • Leverage hapi.js plugins to better structure your codebase

  • Simplify your security workflows with the built-in authentication and authorization functionality of hapi.js

  • Ensure application reliability with testing and code coverage

  • Reduce code complexity using reusable validation logic with joi

  • Gather insight into your application performance via logging

  • Start the journey to building robust production-ready applications

  • In Detail

    This book will introduce hapi.js and walk you through the creation of your first working application using the out-of-the-box features hapi.js provides. Packed with real-world problems and examples, this book introduces some of the basic concepts of hapi.js and Node.js and takes you through the typical journey you'll face when developing an application. Starting with easier concepts such as routing requests, building APIs serving JSON, using templates to build websites and applications, and connecting databases, we then move on to more complex problems such as authentication, model validation, caching, and techniques for structuring your codebase to scale gracefully. You will also develop skills to ensure your application's reliability through testing, code coverage, and logging.

    By the end of this book, you'll be equipped with all the skills you need to build your first fully featured application. This book will be invaluable if you are investigating Node.js frameworks or planning on using hapi.js in your next project.

    Style and approach

    This book takes a step-by-step approach to building an application or web server using hapi.js though examples.

    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 If you purchased this book elsewhere, you can visit and register to have the code file.

    Table of Contents

    1. Getting Started with hapi.js
      1. Table of Contents
      2. Getting Started with hapi.js
      3. Credits
      4. Foreword
      5. About the Author
      6. About the Reviewer
        1. eBooks, discount offers, and more
          1. Why subscribe?
      8. 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. Downloading the color images of this book
          3. Errata
          4. Piracy
          5. Questions
      9. 1. Introducing hapi.js
        1. Node.js – a prerequisite to learning hapi.js
        2. The hapi philosophy versus other frameworks
          1. Building value, not infrastructure
          2. Configuration is better than code
          3. Separation of business logic from the transport layer
          4. Open source and community-centric
          5. Ecosystem
          6. Small modules
          7. Summarizing hapi
        3. Creating our first hapi.js server
          1. Installing hapi
          2. Our first hapi server
          3. hapi plugins
          4. hapi configuration
          5. The hapi style guide
        4. Summary
      10. 2. Adding Functionality by Routing Requests
        1. Server routing
          1. Route configuration
            1. Method
            2. Path
            3. Description
            4. Validate
            5. Pre
            6. Handler
            7. Cache
          2. Route configuration summary
        2. hapi routing algorithm
        3. hapi request life cycle
          1. Extending request life cycle events
        4. hapi request object
        5. The reply interface
          1. Custom handlers
        6. Serving static files with inert
        7. Serving templates with vision
          1. Vision configuration
            1. Engines
            2. Paths
            3. compileMode
          2. Vision summary
        8. Summary
      11. 3. Structuring Your Codebase with Plugins
        1. Structuring applications
        2. hapi plugins
          1. Encapsulating functionality within plugins
          2. Plugin options
        3. Exposing and consuming plugins
          1. Managing plugin dependencies
          2. Exposing sandboxed functionality
          3. Combining plugins
        4. The hapi plugin ecosystem
          1. Authentication
          2. Documentation generation
          3. Logging
          4. Utilities
        5. Composing applications from plugins
        6. Summary
      12. 4. Adding Tests and the Importance of 100% Code Coverage
        1. Why is the importance of testing overlooked?
        2. The benefits and importance of testing code
        3. Introducing hapi's testing utilities
          1. Installing lab and code
            1. Local versus global modules
          2. Our first test script
          3. Testing approaches
        4. Testing hapi applications with lab
          1. Code coverage
          2. Linting
        5. Summary
      13. 5. Securing Applications with Authentication and Authorization
        1. Authentication
          1. Configuring authentication
          2. Cookie authentication
          3. Third-party authentication
          4. Authentication summary
        2. Authorization
        3. Summary
      14. 6. The joi of Reusable Validation
        1. An introduction to validation
        2. Introduction to joi
        3. Validating hapi routes with joi
          1. Validating route responses
        4. Documentation generation
        5. Summary
      15. 7. Making Your Application Production Ready
        1. Persisting data
          1. MongoDB
          2. The 'M' in MVC
        2. Introducing caching
          1. Server methods
          2. Caching with server methods
        3. Introducing logging
        4. Debugging applications
          1. The console
          2. The Node debugger
          3. The debug mode in hapi
          4. Profiling heapdumps
          5. TV
          6. Debugging summary
        5. General production advice
        6. Further learning
          1. Getting started with hapi.js GitHub repo
          3. Makemehapi
          4. The hapi community
            1. The hapi discussion repo
            2. Stack Overflow
            3. hapi Gitter channel
            4. @hapijs on Twitter
            5. Read the code!
        7. Summary
      16. Index