Node V

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…


Modules are more or less like a Class in OOP languages and the modularization makes Javascript code less complex and the separation of concerns leads you to a less complicated maintenance.

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:

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!

2017-09-02 (3)

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’?).

2017-09-02 (4)

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!

2017-09-02 (5)

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.

2017-09-02 (6)

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!

2017-09-02 (7)

2. Calling the funcion directly at the ‘console.log’: console.log(msg()); – It works too!

2017-09-02 (8)


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:

2017-09-02 (9)

At ‘app.js’ I required the ‘server’ module, and when I saved… It works!

2017-09-02 (11)


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.

2017-09-02 (12)

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.

2017-09-02 (13)

Finally, I required the route module at the app.js, passing the ‘app’ variable (that holds the Express) as a parameter.

2017-09-02 (14)

Save, run the server and everything is working fine!

Next post, connecting to a database!

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s