Introduction to Node.js

A tutorial to server-side JavaScript

Web development has become a pretty popular field in Computing. It ranges from designing the look and feel of a webpage, to writing services that processes data via database communication. JavaScript is my language of choice when creating websites and applications because of its flexibility and the vast amount of resources available.

But how does JavaScript become the main language for a web developer? How do you process server-side logic? Enter Node.js.

Node.js is the missing link that you’ve been looking for. It enables you to run JavaScript via the command line, and it opens up the little language you used for a slider plugin into something completely different.

Why should I use Node.js?

There are many server-side languages and frameworks that you could use for your project, but if you want to keep the frontend and backend in the same, easy to use language then it’s definitely worth looking into. It’s also very quick to setup a project with lots of advanced features that could take you a while with other basic tools.

How do I install Node.js?

Node.js can be installed on most operating systems with ease. You can download for Windows here.

# Linux
sudo apt-get install nodejs
# macOS
brew install node

You can find a detailed installation guide via the Node.js website.

How do I run Node.js?

It’s as simple as running the command node <filename>on the command line. This will make any JavaScript in your file execute instead of in a browser window. Pretty simple and handy, right?

Here’s an example of it in action if you’re still unsure. Let’s call this file helloworld.js .

var message = 'Hello World!';

This should be self explanatory — this will output the message to the console. In the browser this would show in the developer tools section as a message that would be pretty helpful for debugging, but in Node.js this would actually output to the command line instead. You can execute this in Node by running node helloworld in the directory you are in. This should output Hello World! as expected.

Now that you have Node.js working on your machine, you can now make scripts very easily and quickly using JavaScript. Try to write some advanced logic and see what you can make. Familiarise yourself with some basic console commands too, as using Node requires you to use the command line.

In the next tutorial we’ll look at how to make use of package managers and how they can give your script even more functionality.