UNDERSTANDING NODE.JS BY BUILDING COMMAND LINE APPS

UNDERSTANDING NODE.JS BY BUILDING COMMAND LINE APPS

<strong>Originally published by Chimezie Enyinnaya </strong><em>at&nbsp;</em><a href="https://pusher.com/tutorials/node-command-line-app" target="_blank">pusher.com</a>

In this tutorial, I’ll be showing how to build command line applications using Node.js.

JavaScript is very popular for building all kinds of applications, from frontend applications to backend and mobile applications. The language keeps evolving and now you can build command line applications or control hardware devices like drones with JavaScript.

Prerequisites

This tutorial assumes the following:

  • Node.js and NPM installed on your computer
  • Basic knowledge of JavaScript and Node.js

What we’ll be building

In this tutorial, we’ll be learning how to build simple command line application. We’ll be building a command line weather application using Node.js. With our command line weather application, we’ll be able to get the current weather in any city in the world. We’ll also be able to get a forecast for any specified city using a different command line parameters.

Getting started

Let’s start by setting up our project and installing dependencies:

    $ mkdir weatherCLI
    $ cd weatherCLI
    $ npm init -y

This will create an empty directory called weatherCLI that will hold all our project files. After our project directory has been created, we change the directory to the project directory and initialize our project. A package.json file will be created and now we can proceed to install our project dependencies:

    $ npm install commander node-fetch --save

Commander is a Node.js library that makes writing command line applications easy. We’ll be leveraging the library to build our command line application. node-fetch is also a light-weight module that allows us make requests to external services to fetch data. We’ll use it to fetch weather data from APIXU.

Obtaining API keys

To obtain our API keys, we need an account on APIXU. Once you’ve created an account, login to the dashboard to find your API key that looks like 4a8****3e``*e248ac1*****``04.

Building the command line app

First, let’s create our project files. Create a src directory, and within it, create two files:

    $ touch src/index.js src/commands.js

Once the files are created, we’ll go ahead to create our commands for the app by adding the code below inside src/commands.js:

    // src/commands.js

    const fetch = require("node-fetch");
    const APIXU_KEY = "<YOUR APIXU KEY>";

We import the node-fetch library, then we create an APIXU_KEY variable, which holds our API key.

Next, we’ll go ahead to create two functions: fetchNow() to fetch the current weather for any specified city and weatherForecast() to fetch the forecast of any specified city likewise:

    // src/commands.js

    const fetchNow = async (city) => {
      const response = await fetch(`https://api.apixu.com/v1/current.json?key=${APIXU_KEY}&q=${city}`);
      const data = await response.json();

      const now = {
        location: data.location.name,
        country: data.location.country,
        longitude: data.location.lon,
        latitude: data.location.lat,
        temparature: data.current.temp_c,
        condition: data.current.condition.text
      }

      console.log(now)
    };   

The fetchNow function is an asynchronous function that will return a Promise. It takes the passed cityas a parameter in order to make the complete API request. The await operator can only be used inside an asynchronous function and it returns the fulfilled value of the Promise, or the value itself if it's not a Promise.

The now object holds just the necessary information we need from the data returned from the API request. The same thing applies to the weatherForecast function:

    // src/commands.js

    const weatherForecast = async (city) => {
        const response = await fetch(`https://api.apixu.com/v1/forecast.json?key=${APIXU_KEY}&q=${city}`);

      const data = await response.json();

      console.log(data.forecast)
    };

After defining all functions, we need to export them so they can be used in our command line program:

    // src/commands.js

    module.exports = {
      fetchNow,
      weatherForecast
    };

Our main application file src/index.js will hold the main application logic. Add the code below inside src/index.js:

    // src/index.js

    const program = require('commander');
    const { fetchNow, weatherForecast } = require('./commands');

We import the commander library and also the defined functions from src/commands.js. After the dependencies have been imported, we’ll define our program parameters:

    // src/index.js

    program
      .version('0.0.1')
      .description('Command line Weather Application')

    program
      .command("now <city>")
      .alias('n')
      .description('see the current weather in the specified city')
      .action(city => fetchNow(city));

    program
      .command("forecast <city>")
      .alias('f')
      .description('see the weather forcast of a specified city')
      .action(city => weatherForecast(city)); 

Let’s go over what each piece of the code above does:

  • .version allows us define the current version of our command line application
  • .command defines how the parameter should be passed to the program with any extra arguments
  • .alias defines an alternative short form parameter that can be passed to the program instead of typing a longer one.
  • .description is the description of the alias
  • .action will call whatever function is passed to it. The action runs the functions and displays whatever is returned from the function. The action is also responsible for handling interactions with the user if the command line application is an interactive application.

In order to read all parameters and arguments passed to our program, we have to parse the inputs after the program command. Add the snippet below as the last line inside weatherCLI.js:

    // src/index.js

    program.parse(process.argv)

Running and testing the CLI application

To run and test our application, run the command below:

    $ node src/index.js now lagos

It will return the current weather in Lagos as in the image below:

To fetch the weather forecast for Lagos, run:

    $ node src/index.js f lagos

Also, we can run additional commands as below:

    $ node src/index.js --help

This will return the application usage menu.

    $ node src/index.js --version

This will return the application version as specified earlier in src/index.js.

Conclusion

From the tutorial, we can see how easy it is to create command line applications using the Node.js Commander library. We’ve been able to create a command line application that can display the current weather of any city in the world using the APIXU. You can work more on the display formats so the displayed data looks interesting to users and also add a network loader while the application is fetching the weather reports.

The complete code for this tutorial is available on GitHub.

Angular 9 Tutorial: Learn to Build a CRUD Angular App Quickly

What's new in Bootstrap 5 and when Bootstrap 5 release date?

Brave, Chrome, Firefox, Opera or Edge: Which is Better and Faster?

How to Build Progressive Web Apps (PWA) using Angular 9

What is new features in Javascript ES2020 ECMAScript 2020

How to Use Express.js, Node.js and MongoDB.js

In this post, I will show you how to use Express.js, Node.js and MongoDB.js. We will be creating a very simple Node application, that will allow users to input data that they want to store in a MongoDB database. It will also show all items that have been entered into the database.

Node.js for Beginners - Learn Node.js from Scratch (Step by Step)

Node.js for Beginners - Learn Node.js from Scratch (Step by Step) - Learn the basics of Node.js. This Node.js tutorial will guide you step by step so that you will learn basics and theory of every part. Learn to use Node.js like a professional. You’ll learn: Basic Of Node, Modules, NPM In Node, Event, Email, Uploading File, Advance Of Node.

Top 7 Most Popular Node.js Frameworks You Should Know

Node.js is an open-source, cross-platform, runtime environment that allows developers to run JavaScript outside of a browser. In this post, you'll see top 7 of the most popular Node frameworks at this point in time (ranked from high to low by GitHub stars).