Node.js Hello World!

A sprint through your first Node.js project.

Scroll down...




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:

  • INFO

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.

Getting Started

  1. Fork and clone the project's Github Repo.
  2. cd into the project directory and run $ npm init to initialize the project
  3. Don't forget to create a .gitignore and add the node_modules/ folder to it!
  4. Now we're ready to get started!

Warming Up

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!

  1. Test that your project is working by logging a simple "Hello world!" and running node index.js.
  2. Once you've verified that your project is working, it's time to install some packages
    • Install chalk with npm install --save chalk
    • Install lodash with npm install --save lodash
  3. Using require in your index.js file, require both chalk and lodash.
  4. Verify that chalk is working by outputting colorized text to the console. You should be able to figure this out from the docs on the link above
  5. Verify that lodash is working correctly by using it to iterate over a simple array and output the value with the lodash _.each function
  6. Once you're done verifying these packages work, commit!

Building the Logger

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:

  1. A log method that takes 2 parameters
    • The first parameter should be the message to log
    • The second parameter should be a string specifying the logging level (INFO, WARNING, ERROR)
  2. An info method that only takes a single parameter for the message and outputs it at the info level
  3. A warning method that only takes a single parameter for the message and outputs it at the warning level
  4. An error method that only takes a single parameter for the message and outputs it at the error level
  5. Based on the logging level, colorize your output using chalk according to the following chart
    • INFO: blue
    • WARNING: yellow
    • ERROR: red

Here is an example image of the colorized output for the various levels of logging:

Node Hello world! sample logger output

Testing Your Logger

  1. Export the module from the file and require your logger in index.js
  2. Verify that you can log a message at using all logging levels
  3. Verify that all logging levels are output with the above colors
  4. Commit!

Logging from a JSON File

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!

  1. Require the JSON file
  2. Require lodash to make iterating over the JSON object easier
  3. Use the lodash _.each function to iterate over the JSON
  4. Output the message for each entry at the given logging level
  5. Verify that the JSON is output correctly and each message is colorized according to its logging level
  6. Commit!

Finishing Up

  1. Commit, push, and submit a pull request back to the main repo
Solution 300 Pull request 300 Octocat 300

Sign up to track your progress for free

There are ( ) additional resources for this lesson. Check them out!

There are no additional resources for this lesson just yet!

Sorry, comments aren't active just yet!

Next Lesson: Introduction to Asynchronous Programming