Getting Started with hapi.js
eBook - ePub

Getting Started with hapi.js

  1. 156 pages
  2. English
  3. ePUB (mobile friendly)
  4. Available on iOS & Android
eBook - ePub

Getting Started with hapi.js

About this book

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.

Tools to learn more effectively

Saving Books

Saving Books

Keyword Search

Keyword Search

Annotating Text

Annotating Text

Listen to it instead

Listen to it instead

Information

Getting Started with hapi.js


Table of Contents

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

Getting Started with hapi.js

Copyright © 2016 Packt Publishing
All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews.
Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author, nor Packt Publishing, and its dealers and distributors will be held liable for any damages caused or alleged to be caused directly or indirectly by this book.
Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information.
First published: April 2016
Production reference: 1060416
Published by Packt Publishing Ltd.
Livery Place
35 Livery Street
Birmingham B3 2PB, UK.
ISBN 978-1-78588-818-2
www.packtpub.com

Credits

Author
John Brett
Reviewer
Jan Lehnardt
Acquisition Editor
Reshma Raman
Content Development Editor
Mehvash Fatima
Technical Editor
Dhiraj Chandanshive
Copy Editor
Sonia Mathur
Project Coordinator
Kinjal Bari
Proofreader
Safis Editing
Indexer
Tejal Daruwale Soni
Production Coordinator
Melwyn Dsa
Cover Work
Melwyn Dsa

Foreword

hapi is a toolkit for web applications (and other HTTP servers) built by people with twice my experience and at least 10 times the smarts. They got the right team together and managed to work with the freedom to get it right this one last time—and they delivered.
hapi is also built by people who understand how to create, foster, and maintain a healthy and inclusive open source community, manage a responsible yet speedy release process, and be all around lovely people. If you are an end user or want to start contributing to the hapi ecosystem, you'll have great fun on your way with the clever technology and, even more so, with the great people.
From the first examples in the first chapter that you work through on your own to a full­fledged system that a large team can work on productively without stepping on each other's toes, Getting Started with hapi.js takes you on the journey to master everything you need to build everything from a hobby project to the most popular site on the Web.
The third step of my "usual process" of working with hapi is this: loving hapi even more.
I know you will, too.
Jan Lehnardt
CEO, neighbourhood.ie

About the Author

John Brett is a software engineer passionate about new technologies, open source, and building useful software while travelling the world. Starting out in Ireland with a degree in Computer Science and Software Engineer, he went on to join IBM through their premier internship Extreme Blue, later working on collaboration software as part of IBM's Software Group. There, John mainly worked with PHP and JavaScript, only working with Node.js in his spare time.
Later in his career, John moved to a growing SAAS company called D4H Technologies. As a lead engineer there, he developed tools to aid emergency response teams globally, all from a lighthouse in Dublin, Ireland. Here, John got more exposure to Node.js, leading D4H's first venture into Node using hapi.js, later joining the hapi.js core team

About the Reviewer

Jan Lehnardt is a developer and business person from Berlin, Germany. He is the Vice President of Apache CouchDB (http://couchdb.apache.org), the database that syncs at the Apache Software Foundation, and a co-inventor of Hoodie (http://hood.ie), the web app framework for frontend developers. Jan is the cofounder and CEO of Neighbourhoodie Software, a product and support company based on Hoodie, CouchDB, and general web-nerdery in Berlin. They are also behind http://greenkeeper.io, a service that sends you a pull request when any of your npm dependencies get an update. In his spare time, Jan co-organises JSConf EU, Europe's favorite JavaScript conference.

www.PacktPub.com

eBooks, discount offers, and more

Did you know that Packt offers eBook versions of every book published, with PDF and ePub files available? You can upgrade to the eBook version at www.PacktPub.com and as a print book customer, you are entitled to a discount on the eBook copy. Get in touch with us at for more details.
At www.PacktPub.com, you can also read a collection of free technical articles, sign up for a range of free newsletters and receive exclusive discounts and offers on Packt books and eBooks.
eBooks, discount offers, and more
https://www2.packtpub.com/books/subscription/packtlib
Do you need instant solutions to your IT questions? PacktLib is Packt's online digital book library. Here, you can search, access, and read Packt's entire library of books.

Why subscribe?

  • Fully searchable across every book published by Packt
  • Copy and paste, print, and bookmark content
  • On demand an...

Table of contents

  1. Getting Started with hapi.js

Frequently asked questions

Yes, you can cancel anytime from the Subscription tab in your account settings on the Perlego website. Your subscription will stay active until the end of your current billing period. Learn how to cancel your subscription
No, books cannot be downloaded as external files, such as PDFs, for use outside of Perlego. However, you can download books within the Perlego app for offline reading on mobile or tablet. Learn how to download books offline
Perlego offers two plans: Essential and Complete
  • Essential is ideal for learners and professionals who enjoy exploring a wide range of subjects. Access the Essential Library with 800,000+ trusted titles and best-sellers across business, personal growth, and the humanities. Includes unlimited reading time and Standard Read Aloud voice.
  • Complete: Perfect for advanced learners and researchers needing full, unrestricted access. Unlock 1.4M+ books across hundreds of subjects, including academic and specialized titles. The Complete Plan also includes advanced features like Premium Read Aloud and Research Assistant.
Both plans are available with monthly, semester, or annual billing cycles.
We are an online textbook subscription service, where you can get access to an entire online library for less than the price of a single book per month. With over 1 million books across 990+ topics, we’ve got you covered! Learn about our mission
Look out for the read-aloud symbol on your next book to see if you can listen to it. The read-aloud tool reads text aloud for you, highlighting the text as it is being read. You can pause it, speed it up and slow it down. Learn more about Read Aloud
Yes! You can use the Perlego app on both iOS and Android devices to read anytime, anywhere — even offline. Perfect for commutes or when you’re on the go.
Please note we cannot support devices running on iOS 13 and Android 7 or earlier. Learn more about using the app
Yes, you can access Getting Started with hapi.js by John Brett in PDF and/or ePUB format, as well as other popular books in Computer Science & Computer Engineering. We have over one million books available in our catalogue for you to explore.