Modules and CommonJS
When you start with Express and EJS, the words “Modules” and “CommonJS” started to be more frequent at documentation, but, what the heck are this things?
In a nutshell…
Express and EJS are Modules.
Is a format that define how to build (or code) these modulesthat allows them to be used at Node.js project.
Writing a Module
Is pretty simple and pretty straight forward! Follow these steps:
1. Create a new JS file and name it. Mine is ‘MyModule.js’ and I saved it on the root of application.
2. Write the code following the CommonJS directives for the module (here is the documentation: https://nodejs.org/docs/latest/api/modules.html).
My module will return a simple message, so I declared a variable named ‘msg’ that recieves the string of my message.
Finally, the module must assing to the property module.exports the value of the message.
I’ll work, believe!
Consuming the Module
Open app.js file and, as we do to use the ‘Express’ Module, we must require the ‘MyModule’ module.
The variable ‘msg’ now holds the ‘MyModule’ module. Note that, as I saved it on the root, I must to put ‘./’ before the module name to show that the module is at the root of the application (or the correct is ‘is at the application’s root’?).
Quick test: I changed the message ‘Listening on port 80’ (or at port 80? hahaha!) and put the msg variable on console.log() and the result is… It works!
The modules can also return a function! And, let’s change the ‘MyModule’ module (bad name, I know…)
I have writed an anonimous function and, when I saved it and the server restarts… Bam! Look at the log! A ‘[Function]’ was logged.
Simple! I need to call this function to get the return! I can do it in two ways:
1. Opening and closing parentheses at the end of ‘require’ and run the function at ‘require’: var msg = require(‘./MyModule’)(); – Look, it works!
2. Calling the funcion directly at the ‘console.log’: console.log(msg()); – It works too!
Starting the server
With this new knowledge, I’ll put the routes and the server start on a Module!
To do this, I created a new folder named ‘config’ and, inside of the folder, I created a file named ‘server.js’
On ‘server.js’, I put all the code needed to start the server:
At ‘app.js’ I required the ‘server’ module, and when I saved… It works!
Let’s do the same to the routes! I created a new folder called ‘app’ and, inside the ‘app’ folder, I created another folder called ‘routes’.
I also moved the ‘views’ folder to ‘app’ folder.
As Express search for views in ‘views’ folder that must be at the root of application, I needed to do a new configuration on the ‘server’ module that tells to Express where to search for the views.
In ‘routes’ folder, I put a new file named home.js and setted up the route for the ‘home’ view.
Here, one more detail to pay attention! The ‘get’ function used to set the route to a view is an Express function, so, the new module that will deal with the route, must recieve the Express as a parameter.
Finally, I required the route module at the app.js, passing the ‘app’ variable (that holds the Express) as a parameter.
Save, run the server and everything is working fine!
Next post, connecting to a database!