
Today we are kicking off my LTM (Learning through Making) series of tutorials to delve into the amazing world of Node.js. We’re going to do some fun projects along the way, but we first need to lay a foundation and get Node.js installed.
What is Node.js?
As described on Wikipedia, “Node.js is an open-source, cross-platform runtime environment for developing server-side Web applications.” Node.js is awesome because it allows us to build a gamut of applications from highly scalable networked applications to console applications. Node.js uses a single-threaded event loop model to process events in an event queue which helps it process high amounts of I/O. This differs from say, the Apache web server, which uses a thread for each new connection and can thus not handle so many concurrent connections. Node.js is based on JavaScript and is further positioning JavaScript as a universal language that every developer should know. Let’s be on a first name basis with “Node.js” and simply call it “Node” from here on out.
Let’s Get Started
Without any further ceremony, let’s jump in and install Node. In our next post, we will install Node on the Raspberry Pi since the RasPi is truly a fantastic platform for building fun and affordable projects. This time around, we will focus on getting Node up and running on Windows and Linux in general.
Visit the Node downloads page to download a version that is appropriate for your platform. If you are running Windows, I recommend downloading the 64-bit .msi package unless you are on a 32-bit machine. For Linux users, you could get Node.js from your package repository such as apt (Debian) or yum (Red Hat), but you will probably get an old version of node from the default repositories. The updates move fast in the Node.js world!
On the downloads page, you will see an option to either download the LTS (Long Term Support) version which is “mature and dependable” or the Current version which includes the “latest features”. Let’s go with the Current version so we can get more of the modern JavaScript language features. It will be more fun too! 🙂 You may opt for the LTS version if you are deploying Node in a production environment.
Go ahead and install Node now and then we’ll try it out.
Node Test Drive
Let’s take Node for a quick spin to verify the installation and see it in action. Go ahead and launch a command prompt and type the following:
C:\> node
This will launch the Node REPL (Read Eval Print Loop) which will enable you to enter commands in an interactive mode. This is similar to REPLs that are available in other languages such as Python. We now have a calculator that we can use. What more could we want? 🙂 Let’s try it out:
>> 1 + 3
4
Very nice! Let’s save a value to a variable and confirm it is stored in memory:
>> msg = 'hello'
'hello'
> msg
'hello'
Next, let’s create a JavaScript array and check the length:
|
|
Code test
package main
import "fmt"
func main() {
fmt.Println("Hello, World!")
}
$ ls -la
total 8
drwxr-xr-x. 1 dave dave 194 Nov 22 11:16 .
drwx--x---+ 1 dave dave 2268 Nov 22 13:42 ..
drwxr-xr-x. 1 dave dave 20 Oct 27 14:01 archetypes
-rwxr-xr-x. 1 dave dave 3594 Nov 22 14:01 config.yaml
drwxr-xr-x. 1 dave dave 180 Nov 14 20:15 content
drwxr-xr-x. 1 dave dave 8 Nov 7 19:52 data
drwxr-xr-x. 1 dave dave 122 Nov 18 08:48 .git
-rwxr-xr-x. 1 dave dave 93 Oct 28 11:13 .gitmodules
-rwxr-xr-x. 1 dave dave 0 Oct 27 14:04 .hugo_build.lock
drwxr-xr-x. 1 dave dave 0 Oct 27 14:01 layouts
drwxr-xr-x. 1 dave dave 6538 Nov 22 13:42 public
drwxr-xr-x. 1 dave dave 8 Oct 27 14:04 resources
drwxr-xr-x. 1 dave dave 304 Nov 10 16:10 static
drwxr-xr-x. 1 dave dave 8 Oct 28 11:13 themes
Finally, let’s step it up a few notches and iterate over the array using the ES2015 arrow function:
> fruits.forEach(f => console.log(f))
banana
apple
orange
undefined
The Node REPL will also return “undefined” in this context since JavaScript functions always return something and the REPL is treating this entry as a function. You can safely ignore this.
To exit the Node REPL, press Ctrl-C, and then press Ctrl-C again.
What’s Next?
In our next article, we will get Node up and running on the RasPi. After that, we will leverage the goodness of the npm (Node Package Manager) community and build a simple, but highly functional Web server without any code.