Modularizing our code
If you take a look inside the src/index.js
file, you'll see that there are three top-level middleware functions—checkEmptyPayload
, checkContentTypeIsSet
, and checkContentTypeIsJson
—as well as an anonymous error handler function. These are prime candidates that we can extract into their own modules. So, let's get started!
Modularizing our middleware
Let's carry out this refactoring process in a new branch called create-user/refactor-modules
:
$ git checkout -b create-user/refactor-modules
Then, create a directory at src/middlewares
; this is where we will store all of our middleware modules. Inside it, create four files—one for each middleware function:
$ mkdir -p src/middlewares && cd src/middlewares $ touch check-empty-payload.js \ check-content-type-is-set.js \ check-content-type-is-json.js \ error-handler.js
Then, move the middleware functions from src/index.js
into their corresponding file. For example, the checkEmptyPayload
function should be moved to src...