What we have developed so far is a simple HTTP server application that listens and processes known request types; however, it is not so well structured, as the functions handling the requests are not reusable. Node.js supports modules embracing code isolation and reusability.
A user-defined module is a logical unit consisting of one or more related functions. The module can export one or more functions to other components while keeping other functions visible only to itself.
We will rework our HTTP server application in such a way that the entire request handling functionality will be wrapped in a module. The module will export only a generic handler function that will take a request object as argument and, based on its request type, it will delegate the handling to inner functions not visible outside the module.
Let's start by creating a new module directory...