What is Node.js and Should I Learn it in 2021?

Node.js logo

Node.js is a JavaScript runtime which uses the Chrome V8 JavaScript engine. JavaScript is a client-side language which primarily relies on the browser to interpret and run the code. However, this meant that JavaScript could not run anywhere else such as server-side.

This is why Node.js came to save the day!

Node.js came out in 2009 to enable people to run JavaScript outside of just the browser.

What is a Runtime?

A runtime is a way of compiling the code and making it readable by the computer. Node.js will look at the JavaScript code and start to transform it into machine code which your computer can understand and therefore, execute.

How to Install Node.js

Go to the official Node.js website and download the LTS version which is recommended for most users.

Once that has installed you should be able to see what version of Node.js you are running by typing 'node -v' in the terminal.

node -v
// Returns 'v14.16.0' as of 17th May 2021

If you are getting a version number after typing that command, you are officially running Node.js. Congrats!🎉

Is Node.js Easy to Learn?

Node.js should have a nice easy learning curve if you are familiar with JavaScript since it uses JavaScript syntax but just executes the code differently to how we would in the browser.

Let's build a Node.js app to show you what I mean.

Example console.log JavaScript code

This code will look identical to what you would write normally in JavaScript. The only difference is how we execute that line of code.

Instead of using the browser, we can execute this code in the terminal.

Running JavaScript in the terminal

What Can Node.js Be Used For?

Node.js is a great choice to use for building API's and server-side logic. Let's build a server that uses Node.js!

To start creating a Node.js server you must run 'npm init' in the terminal of your JS application.

NPM is Node Package Manager which is used to manage dependencies that your application relies on. It is what we use to install, update and delete packages that you use in your app. NPM comes installed with Node.js so you don't have to worry about installing it separately.

Terminal showing 'npm init'

This is the sort of information you can modify in the 'npm init' process. I just left it all to the defaults which you can also do by typing:

npm init -y

After you have run this command you will want to install Express which is a Node.js framework that makes it quicker easier to get a server up and running.

This can be doing by typing:

npm install express

We can build a simple server that sends back a response when you visit "localhost:3000".

Express.js server

Let's break this code down!

Lines 1-2 is showing us requiring the Express package and assigning it to a variable called 'app' which we can use later.

Lines 4-6 is using the 'app' variable to run a function built in to Express which gets the index page and sends back a response containing a HTTP status code 200(OK) and some text "Hello world!".

The 'app.get' function has 2 parameters. One is the "/" which indicates the index page and the second is parenthesis with 'req' and 'res' in them. The 'req' is short for request which can get information from the incoming request from the client. The 'res' is short for response, which is what you can send back to the client when they visit index page.

Lines 8 - 10 is using the 'app.listen' function which takes in a parameter of 3000 meaning that we will listen to any incoming requests on port 3000. You can create a callback function like I have and send back a console log to say the 'app.listen' was successful.

Are you ready to see what all 10 lines of code creates on localhost:3000?

Example code in action

Impressive right? 😅

Don't worry there is a lot more that goes on under the hood!

Node.js comes with lots of cool modules to help you bring out your server-side app to life. Why not have a go at creating a Node.js API that will send back data from MongoDB or a MySQL database.

Here's some good resources I'd recommend when you are wanting to learn more about Node.js and API's.


So should you learn Node.js? If JavaScript is your main programming language and you are getting into web development, then Node.js is great to learn to start widening your skill set to become capable of building full-stack applications.

Why learn a totally different programming language in the beginning to start building full-stack apps, when you could be using the precious JavaScript knowledge you already have!

Join the mailing list!