Finally, it is time for you to build your first project using Node.js! In this project, you'll flex your muscles on the core concepts we've covered this section.
Your task is to build a logger. This will be a module that you create that logs message at three logging levels:
Further, you're going to include a package using NPM to colorize your logger's output. Then you read in some JSON to output various messages using your brand new shiny logger.
cdinto the project directory and run
$ npm initto initialize the project
.gitignoreand add the
node_modules/folder to it!
Your first order of business is to get your project running. By default, NPM initializes with a target file of
index.js so create a file with that name. Now let's get this app running!
"Hello world!"and running
index.jsfile, require both chalk and lodash.
Now it is time to start building the logger. Your task is to create a logger module. Create a
lib/ folder and put a file called
logger.js in there. This is where you'll be coding up your logger. Your logger will need the following features:
logmethod that takes 2 parameters
infomethod that only takes a single parameter for the message and outputs it at the
warningmethod that only takes a single parameter for the message and outputs it at the
errormethod that only takes a single parameter for the message and outputs it at the
Here is an example image of the colorized output for the various levels of logging:
Now that your logger is built and ready to use, your next task is to require the JSON file in the
data/ folder so that you can output the messages. Each message has a
"level" key so be sure to output the message using the correct level in your logger!
_.eachfunction to iterate over the JSON