node.js module patterns
27 Jul 2015In today’s post, I’ll walk through some of the more common node.js module patterns that you can use when writing modules.
Exporting a function
Exporting a function from your module is a very procedural way to go about things. This allows you to treat your loaded module as a function itself.
You would define your function in your module like so:
You can then use your module as if it were a function:
Exporting an object
Next up, you can pre-assemble an object and export it as the module itself.
You can now start to interact with your module as if it were an object:
Exporting a prototype
Finally, you can export an object definition (or prototype) as the module itself.
You can now create instances from this module: