Book Image

Mastering Web Application Development with Express

By : Alexandru Vladutu
Book Image

Mastering Web Application Development with Express

By: Alexandru Vladutu

Overview of this book

Table of Contents (18 chapters)
Mastering Web Application Development with Express
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Chapter 5. Reusable Patterns for a DRY Code Base

When creating web applications with Express or Node in general, there are situations that keep repeating, and we handle them by writing the same code over and over again. In this chapter, we will create a sample application and then refactor it to eliminate the duplicated logic using the existing NPM modules.

In this chapter, we will cover the following topics:

  • Eliminating the if (err) {} pieces of code spread throughout our application

  • Using control-flow modules instead of counters and avoiding a tree-like code structure

  • Ensuring that the callback method execution only happens once

  • Extending objects in a reusable way with properties

  • Creating custom errors without having to manually define them and their properties each time

The repetitive code portions found in our sample application are common to most Express applications. They are not something specifically catered for the purpose of this chapter alone.