2. Express.js Lessons. Logger, Configuration, Templating with EJS. Part 1.


Hey all! To develop our app further, we need to do two more vital things – configuration and logging. We will use the nconf  module for configuring:

npm i nconf

It is definitely a weak idea to write port 3000 in the app and further specify connection to the database and so on. So, we put nconf  and now can look at documentation. We can configure the module, which means we connect it and then tell to this module where it can read configuration. The line is:

Which means: read configuration from the command line, environment variables and files. Let us now create config directory, create index.js in it and connect nconf there. Next copy configuration from the documents into it changing it a little bit (the file will be from the current directory), specify the path:

In the first config (create the file config inside our new directory  – config.json) let us write: port: 3000.

Attach the config module to app.js:

 change the launch configuration a little bit for it to find this module and add NODE_PATH.
Also, within app.js add the method 

Check it. Everything works!

The next thing we should do is to install a logger:

npm i winston

We will use our own wrap over winston. In order to find the right place for putting it, create the directory libs. Here we will put those modules and files that do not fall out anywhere, but we still some place for them. In our case, logging is the file log.js, (create it), which will be in this directory. Here is an example of such wrapping:

We get the environment. In our file log.js we’ll get the environment directly from NODE_ENV. In order to make it work, let us add the launch configuration with NODE_ENV development.


What does this wrap do? If someone anchors a logger (let us add the record to app.js) , for example:

the function getLogger takes a module and generates a special logger object for it. It may have some transporting means activated or switched off, a correct logging level, and so on. The only difference is a mark. So, let us see what thing it is and how it will work.

Take the logger and do log.info instead of console.log:

Launch it. The logger outputs everything first in different colors, second – if there is development, it outputs the debug level and above, while remaining on production, if outputs error. In this case, we’ve got development, so everything is visible for us.

Moreover, the logger has a mark:


which means what its original file was. Sometimes it is very interesting to know such things. So, we take module.filename and get 2 last elements of the path. Let’s move on now.

We will work on outputting a standard HTML page. We’ve got some Middlewaresbut we will cut them and take the Middlewares built into Express. So, we need the settings:

These are the settings for a templating system, while our template engine will be ejs. Let us change the name of our views directory to templates. You may have any other templating system in your case – in fact, there are many of them. We will get rid of the port because there is obviously no reason to add it inside the app:

Let us do a normal config and take settings out of it. Moreover, we will add other Middlewares that we’ve got. Our app.js will look like:

Please stay tuned, we’ll continue in th nx article!

The lesson code can be found  here.


The materials for this article have been borrowed from the following screencast.

We are looking forward to meeting you on our website soshace.com

About the author

Stay Informed

It's important to keep up
with industry - subscribe!

Stay Informed

Looks good!
Please enter the correct name.
Please enter the correct email.
Looks good!

Related articles

Managing Kubernetes using Terraform

Kubernetes has continued in its strive to influence the tech space with its flexibility and portability in container orchestration. And with its ...

Handling GraphQL API Authentication using Auth0 with Hasura Actions

In this article, we’re going to demonstrate how we can set up Authentication/Authorization with Hasura and Auth0. First, we’ll set up the Auth0 ...

Building Rest API With Django Using Django Rest Framework and Django Rest Auth

Building Rest API seems to be complex when trying to achieve this on your own with Django,  thanks to the Django Rest framework project which has ...

No comments yet

Sign in

Forgot password?

Or use a social network account


By Signing In \ Signing Up, you agree to our privacy policy

Password recovery

You can also try to

Or use a social network account


By Signing In \ Signing Up, you agree to our privacy policy