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

Error checks and callback functions


A well-established pattern with the Node programs is to have an error argument as the first parameter of the callback function, since we cannot use try-catch for asynchronous code and we also don't want our programs to break each time an error occurs.

The most common line found in our sample application is the one that checks for an error and returns early by executing the callback function with the error parameter:

if (err) { return callback(err); }
// or:
if (err) { return next(err); }

If there is no error, the rest of the code that follows will execute.

Luckily for us, there are some modules that can help us avoid having to write this boilerplate code each time we need to delegate the error to the callback function. These modules are available at the following URLs:

Each of these modules provides a function that takes two parameters and should...