If you’ve been learning or reading about web development, you’ve probably heard about Node.js. In this article series, we’ll help you get started using Node by building a homework-tracking application and providing you with all the information and tools you need to develop your own projects using Node. Before that, let’s talk about what Node is and how we can get started using it.
What is Node.js
Why should I use Node.js?
Node.JS is an attractive choice for backend development for several reasons:
Consistency and Efficiency
When should I use node?
The speed and versatility of Node make it an extremely popular choice for building projects and services that require real-time data display. Not only that, but the scalability offered by Node makes it a good choice for anyone, no matter the project size. For instance, on top of the many hobbyists and start-ups using Node to build their products, large companies like Walmart, LinkedIn, Uber, and even Netflix also use Node to deliver their services.
How will we learn about node?
This post is the first in a series about Node.js that will walk you through the syntax and structure of a Node project, get you started using NPM and some of the standard packages that will be useful to you as a Node developer, and introduce you to a node based stack that you can use to develop whole projects of your own. As with many things in programming, one of the best ways to learn is by doing so we’ll be learning how to use Node by creating an application together that, hopefully, we as students will be able to get some use out of: a homework/exam tracker that we’ll call “Due or Die.”
Due or Die will function by allowing users to create accounts under which they can submit a list of courses they are taking for the semester, a list of assignments/exams for each class, and a due date for each assignment. Our app will save this information to a database that we will set up. It will then have a dashboard that users can access after logging in, which will display upcoming due dates and an option to send out email reminders that an assignment due date is coming up. With our application, missed deadlines will be a thing of the past!
Where can I find the code for this project?
The code for Due or Die will be available on my GitHub in its own repository. The repository is called “due-or-die” and can be found here. Before each article is posted, I’ll create a new branch that contains all the code up to and including the latest code from that post. In this way, you’ll be able to see the application grow and expand over time, follow along with each article to build the application out yourself and check your work against mine if you run into any errors as you’re coding.
How to install node
Node.js has an install wizard that we can download to install Node on our Windows machine. The installer will also download and install NPM for us and add both to our system’s path so that we can call Node and NPM from the command line. The most recent version of the installer can be found here. Once your download has finished, just open the installer and follow the instructions in the wizard to complete the installation!
Node.js also has a macOS install wizard that you can use if you’re an Apple user. Simply navigate to the downloads page and click the macOS installer to download the .pkg file instead of the Windows installer. Once downloaded, you’ll just need to run the installer, and you’ll be all set.
Node.js and NPM have been made available in the Ubuntu repository, making it possible to install each from the terminal in just a few commands. In my experience, this is the easiest way to install Node and NPM on a Linux machine and is the way we’ll be using in this guide. To do so, you’ll first need to open a terminal which you can do using the Ctrl + Alt + T keyboard shortcut. Once in your terminal, use the following command to install Node on your device:
sudo apt install node.js
Node will now be installed on your device, but you’ll need to do the same to install NPM since it will not be installed along with Node as it is using the macOS or Windows installers. To do so, you can use the following command:
sudo apt install npm
Testing the Install
Now that we’ve installed Node, we’ll want to test that our installation worked properly and ensure that NPM has also been installed. To do so, we will check the version of Node and NPM that have been installed by opening up a terminal and running the command:
If our installation was successful, we should be met with the Node version installed on our machine. We should check the version of NPM that we have also installed to ensure that it has been installed successfully. To do so, run the command:
Running our first file with node
To get us started, we’ve written a file called
hello_there.js containing a simple print statement (in JS, the syntax to print is console.log(“whatever you’d like to print”)) that will execute when the file is run. The code for this file can be found below, but it is also in the GitHub repository for this series mentioned earlier.
With this command, the code in the file will then execute, and we’ll be met with the printed line, as shown below
In the next post, we’ll expand on what we learned about here by talking about some basic project structures before diving into more detail about NPM, some different available packages, and how we can go about incorporating them into our project.