Command Line Applications in Nodejs
July 22, 2017
Made with: Node
A command line application is a program that you can run from the terminal's command line. You can have the output print directly to the terminal console.
I recently wrote three simple command line applications to practice the concepts learned in Treehouse's (affiliate link) Basic Node course:
- A weather app
- A stock quote app, and
- A currency exchange app
The way these three apps work is this: you call the program from the command line by typing
node weather.js 33155
in the case of the weather app. This will print the temperature and weather conditions for zip code 33155 to the console. You can also pass several arguments, for example:
node weather.js 33155 27516 13902
which will generate three lines of output, one for each zip code entered. The output will look like this:
Temperature in Miami, FL 33155 is 88.5F and it is currently Clear. Temperature in Binghamton, NY 13902 is 80.1F and it is currently Rain. Temperature in Chapel Hill, NC 27516 is 93.6F and it is currently Cloudy.
The stock quote app and the currency exchange app work just the same: you call the program passing along one or more arguments (stock symbols or currencies in abreviated form) and the program will print the output to the console.
This is how the input and the output of the stock quote app look:
node stocks.js GE AAPL FB GE stock price closed at 25.91 AAPL stock price closed at 150.27 FB stock price closed at 164.43
This is how it looks in the currency exchange app:
node fx.js GBP EUR BRL One GBP is equal to 1.2992 US Dollars One USD is equal to 0.769704433497537 GBP One EUR is equal to 1.1642 US Dollars One USD is equal to 0.8589589417625838 EUR One BRL is equal to 0.32138 US Dollars One USD is equal to 3.111581305619516 BRL
What is happening behind the scenes is that the program is using Node's
https.get methods to get information from a JSON file provided by an API (application programming interface). For example, in the case of the weather app, it is using Weather Underground's API. To use an API you need an API key, which will be given to you when you sign up.
If you want to take a look at these three programs under the hood I've placed the source code on Github. You can clone the repository and try to run the apps from your terminal's command line. Remember to replace where it says "yourapikeygoeshere" with your own API key, or the programs will not run (except the currency exchange app, where the API I'm using does not require a key).