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 applicationUsing 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.