Command Line + Node.js

Command Line

What is the Command Line?

Everything in a Unix computer is a file. Every program it runs, every little toolbar, every document you own, and every little nuance about the way it runs, is a file. The command line, simply put, is a place where you can navigate through that file system and read/run those files. Within it, you type commands instead of clicking on buttons in a GUI. The command line can be very powerful, complex, and dangerous, but should be lauded and admired, not feared.

In general, these tutorials will take place on a Mac, because of their ease-of-use and unix-like filesystem. Following along with Linux will be very similar, but Windows might cause some problems (use at your own risk).

Mac users — Open the Terminal using Spotlight, or by going into your /Applications/Utilities folder.

Linux users — You probably already know where your terminal is.

Windows users — Please download and open Babun.

Syntax

A command has three parts: the utility, the flags, and the arguments. The utility is required, and always comes first. The flags are usually optional, and can take both long (--flag) and short (-f) forms. The arguments are usually the last part of the command, and are generally required.

Try running this:

ls -l ~/Documents

In the above example, ls is the utility. It runs a program that lists directory contents. The -l is an optional flag that tells ls to display all the file list in long format. ~/Documents is the argument; it tells ls to list the directory contents at Users/<username>/Desktop (or equivalent on your OS).

You can learn how to use any command by typing man in front of it (man — for manual — is the utility, and the command you want to learn about is the argument. You can move up and down the documentation with your arrow keys, and exit by typing q. Often, but not always, you can also learn about a program by using the flag --help with no arguments, or even just by using the utility with no flags or arguments.

File systems

Every file on your computer is stored in a folder that is part of a larger tree. Let’s look at the following location:

/Users/<username>/Documents/helloWorld.txt

Replace <username> with your login’s username.

The first / is the root of that tree. It is where every file and folder on the computer is stored. Go ahead and see for yourself by typing the command cd / to change your directory to the root /, and see what’s there by using ls.

The next part, Users/, is a folder within /. Go ahead and cd into it.

On my computer, <username>/ is a folder within /Users, and Documents/ is a folder within <username>/.

Finally, a text file called helloWorld.txt is within my Documents/ folder.

You can cd back up a level by doing cd ../, and can go up multiple levels by chaining those ../s. Finally, the shortcut for /Users/<username>/ is ~/ — remember that; ~ (tilde) is your home (directory).

Basic Utilities

Below is a list of basic utilities that most people use regularly. Anything in caps with a $ in front of it is a variable that should be replaced with your specific needs.

man $UTIL displays the manual for the $UTIL.

ls $DIR lists the contents of the $DIR directory.

cd $DIR changes directories to $DIR.

pwd prints the location of the current (working) directory.

cat $FILE prints the contents of the $FILE to the terminal.

cp $FILE $LOCATION copies $FILE to $LOCATION.

mv $FILE $LOCATION moves $FILE to $LOCATION.

rm $FILE removes the $FILE (deletes it).

$CMD1 | $CMD2 pipes (|) the output of $CMD1 into the argument of #CMD2.

sudo $CMD runs the $CMD as the super user (super user do).

sudo can be dangerous, so only use it if you know what the command you’re elevating is going to do. You can delete the entirety of your computer’s hard drive by running sudo rm -rf at the root directory (WARNING: DO NOT DO THAT!!).

APIs

Application Program Interfaces are a set of rules dictating how applications should interact. When discussing APIs in the contet of the web, we are usually referring to the process we use to post information to a website or database or pull information from a website or database.

JSON

JavaScript Object Notation is a data format used extensively on the web. It was developed for easy parsing within JavaScipt. Many of the APIs we will be using will return their data in JSON format.

  • JSON is written in name|value pairs "name":"Jake"
  • Array:

  • Objects can have several sub-object:

  • An object with objects and an array:

Example JSON

Example

  • In your terminal, change to your home directory cd ~
  • Make a new directory called “test_api” mkdir test_api
  • Go into that directory cd test_api
  • Initialize Node Package Manager in that directory npm init
  • Follow the prompts to get it working
  • Open your text and save a new file called test_api.js in the test_api folder

  • paste this code into the new file:

  • We need to know what the JSON will look like before we know how to format it.
  • use the this jsonformatter site to check out your request

Exercise

  • Find a new API from this list of free, public APIs
  • Make a new node.js script that fetches data from the API and parses it

Leave a Reply