NodeJS is a back-end (also known as "server-side") language that facilitates the flow of data between a data-holding layer (like a MySQL database) and a front-end webpage (i.e., what the user sees when they access your webpage). There are lots of options for server-side languages (PHP, Python, Java, Ruby, etc.), but NodeJS is very good at doing real-time application work, so we'll be looking into this since it covers the most use-cases for this class. Again, we're going to be focusing on the Ububtu 16.04 development environment, but these steps can be ported over to just about any Unix system by tweaking which application manager you use.
In order to install NodeJS, we're going to use a piece of software called the "Node Version Manager", or NVM. Generally, you can just install one version of Node onto whatever machine you're going to be using to run the project, but using NVM will allow you to install multiple versions (1.0.0, 1.2.0, and 2.0.0, for example) and switch to them as needed. In this tutorial, we're going to be installing version 7.8.0, which is the most recent stable release at the time of writing.
As usual, before you start, you're going to want to update your
apt-get. Then, we're going to install a few packages your system will need to make things work. (Note: You may already have these packages installed, but you can still safely run these commands regardless).
sudo apt-get update
sudo apt-get install build-essential libssl-dev
Once these packages have been installed onto your system, we are going to need to run a curl request for the NVM script so that we can run it on our system.
curl -sL https://raw.githubusercontent.com/creationix/nvm/v0.31.0/install.sh -o install_nvm.sh
In order for the installation to take effect, you can either restart your machine (or VM), or you can toggle your profile in the terminal with the following command:
Now that we've installed the NVM onto the system, we can test to see if it works by typing
nvm onto the command line. You should see some application-specific output if the installation was successful.
The next thing to do now is install our specified version of NodeJS
nvm install 7.8.0
This does exactly what it says on the tin--it installs NodeJS 7.8.0, which, again, is the most current release at the time of this writing. If this is your first install of NodeJS using NVM, then it will automatically be aliased as your default install, but if it ISN'T your first NodeJS install with NVM, then use the following command to set it as default (don't worry, you can always set another version as default later if you need to).
nvm alias default 7.8.0
NPM is a really valuable tool for ANY sort of web development, but it works especially well with NodeJS applications. NPM stands for "Node Package Manager", and it allows the user to import different "packages" of code into their application. The idea is to facilitate code-reuse.
The nice thing is that you get NPM installed automatically with your new install of NodeJS. NPM is invoked in a very similar way to NVM (watch out for the similar naming!), by using the
NPM command on the command line.
NPM is powerful because it can manage a lot of packages at once, leaving you with more brain-space to think about the actual application, rather than wondering what 20 packages you have installed and whether or not any of those are the package you're currently looking for. Here are some of the most helpful commands that NPM has to offer:
Adding a package to your local project directory:
npm install [package_name]
Removing a package from your local project directory:
npm uninstall [package_name]
Updating packages in your local project directory:
There IS one little trick with NPM: packages can either be installed locally or globally. Notice how in the previous section I explicitly talked about your "local project directory".
The difference between local and global packages is exactly what it sounds like--local packages can only be used within the directory that they are installed in, whereas global packages can be used globally. Generally, to prevent your system getting clogged up with all sorts of packages, only command-line-interfaces are installed globally, although there ARE exceptions to that rule.
Any of the commands from the previous section can be used to install packages globally by adding the
-g flag to your terminal command.
Now that we've learned how to use NPM to deal with all the packages that we'll be using throughout our application, we need to learn about these packages themselves.
Any information you could possibly want about NPM and ALL of its packages can be found within the NPM Documentation.
There are a lot of useful packages within NPM, many of which we aren't going to go over today. Don't be afraid to google (or search NPM's repository site, linked above) for something if you want to see if it is in the NPM repository--you never know what could be in there! Here are some of the packages that at least a few of you will find useful in your NodeJS application:
Data visualization on the web is really important in the modern web and within modern software applications. Luckily for us, there are a lot of libraries that are easy to use that do a lot of the heavy lifting for us. One such library is Canvas.js. Feel free to use any other library that you want, but CanvasJS is fast, free, and looks pretty decent if good-looking data visualization is your kind of thing.
All the examples of charts, graphs, and dashboards are on their site along with code examples, so check here for more information!
Alexander Six, 2017