Learning through Making – Getting Started with Node.js

Learning through Making (LTM) logoToday we are kicking off our 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?

Node.js logoAs 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:

> fruits = ['banana', 'apple', 'orange']
[ 'banana', 'apple', 'orange' ]
> fruits.length
3

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.

ShareTweet about this on TwitterShare on FacebookShare on Google+Share on LinkedInShare on RedditEmail this to someone

Leave a Reply

Your email address will not be published. Required fields are marked *