The Ultimate Node.js Beginner Tutorial

This tutorial is for all developers who want to program with Node.js but don’t know much about Node.js yet. Node.js is becoming more and more popular and every web developer should know at least the basics. After this tutorial you can clearly count yourself in this group.

So that you have a red thread through this Node.js Tutorial, I have you the points times summarized:

Table Of Contents

1. What is Node.js?

With Node.js the scripting language JavaScript has also found its way into server-side programming. Before Node.js JavaScript was mainly responsible for the frontend and the interaction with the visitor. With Node.js you can now develop from small CLI (Command Line) tools to complex programs and HTTP servers. Exactly this universal usability makes Node.js for me personally - and also for many other developers - so ingenious!

If you are wondering if you really need Node.js and want to be able to use it, you should read the most frequently asked questions below and decide if Node.js is the right technology for you.

Another important reason for the success are the on-board resources that come with Node.js. From the ground up, Node.js already has many features and modules that are easy to use. And to make the system even more perfect, there is the package manager - also called npm (Node Package Manager). More information can be found later in the Node.js Module section. Anyway, it’s very easy to add new features, like extensions from other developers.

That sounds promising at first, but can you imagine anything concrete about it? Probably not. At least it wouldn’t be very easy for me if I read something like this on a topic I didn’t know. Therefore read this section best at the end of this article once again and I assure you, you will know what I am talking about! ;)

2. Install Node.js

Before we can start programming, we first have to install Node.js on our computer. Now select the operating system you want to develop on. Windows or Mac…?

2.1 Windows

Under Windows you can simply download the installer and follow the installation instructions. It is best to download the LTS (Long-term support) version as it is already established and has fewer bugs than the latest version.
Windows Installer

2.2 Mac

On a Mac, just like Windows, you can download the official installer and follow the installation instructions. Also download the LTS (Long-term support) version, because it is already established and has less bugs than the latest version.
Mac Installer

2.3 Plesk (Hosting)

If you manage your domains with Plesk and want to host your app there, all you need is the Node.js extension, which you can download with a simple click.

Plesk GUI - Node.js Installation
To develop, however, you should work locally on your Mac or Windows computer. When your app is ready and you want to deploy it, you can use this guide to help you.

3. CLI

CLI stands for Command-Line Interface and means command line. On the Mac you can use the pre-installed program “Terminal” and on Windows the command prompt (cmd.exe).

Windows cmd.exe
With this we can “control” Node.js. With the command node we can now execute any JavaScript command. In the further course of this article you will now know what you can understand by the CLI.

4. Node.js Modules

The crown jewels of Node.js are its modules. At the beginning I already mentioned that Node.js has a package manager. The abbreviation and the command for the CLI is npm (Node Package Manager).

NPM is a gigantic network of development tools that can be downloaded free of charge for your application. If you work with Node.js, you’ll find that you have to rely on other developers’ modules over and over again. You don’t have to reinvent the wheel all the time.

In this example we download the express module:

npm install express

You can also install modules globally, which means it applies to your entire computer and is not only available in one project. Just append the parameter -g.

npm install -g express

When you install a module, the node_modules folder is automatically created. This folder contains all installed modules and can normally be ignored by you. We can include a module in the code later in this way:

const express = require('express');

5. First project

5.1 Create project

The time has come, we’re finally starting programming. To create a Node.js project, we simply create a normal folder in any directory. There we create an index.js, which is our start file.

Node.js Folder structure

5.2 Initialize Node.js App

Now we have to say in this folder that we want to make a Node.js app out of it, we do that with this command:

npm init

We are asked for different parameters like package name, version and description. You can fill in these fields or just leave them at their default values. We can change these settings later at any time.

npm init

5.3 package.json

A package.json file has now been created in our folder. This file contains all information about the author, version and most importantly, about all installed dependencies (modules).

// package.json

{
  "name": "app",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC"
}

5.4 Write program code

Now we can write the first JavaScript code in our index.js. Of course we can’t do without it and have to spend “Hello World”.

// index.js

console.log('Hello World');

5.5 Test application

The first code is written and we want to start our program. This is what we do with this command:

node index.js

We get this result, cool! 🥳

Node Hello World

6. Automatic App Restarts (Nodemon)

It’s really annoying when we have to manually restart our Node.js app after every little change to the code, isn’t it? Right, so there is the module nodemon, which we install globally. This module detects file changes and restarts the Node.js app within milliseconds. This speeds up our workflow enormously.

npm install -g nodemon

After the installation we start Nodemon with this command and immediately our app is started and restarted automatically when changes are made.

nodemon index.js

If you now save your files within the project folder, the Node.js app will automatically restart.

Nodemon Example

7. Browser output

Now we already want to set up our first small web server in order to be able to deliver content in the browser. Therefore we install the module express with the following command:

npm install express

And we adjust our index.js like this:

// index.js

const express = require('express');
const app = express();

app.get('/', (request, response) => {
  response.send('Our first Node.js webserver');
});

app.listen(3000, () => console.log('Server running on port 3000'));

To help you understand everything, here is an explanation line by line:
Line 3: Integration of the express module.
Line 4: Initialization of the express module in the variable app.
Line 6: We intercept the page call from / to our server to perform an action.
Line 7: We send the text “Our first Node.js webserver” back to the requestor as an answer.
Line 9: We start our web server on port 3000 and display a message in the console.

If we ask for browser now our computer on the port 3000, we get this result:

Browser tab Node.js Example

Simple, isn’t it? That’s why Node.js is so brilliant. You only need seven lines of code for your own web server.

If we now adjust our route a little bit, we can return data that has already been transferred:

// index.js

const express = require('express');
const app = express();

app.get('/:yourName', (req, res) => {
  res.send('Your name: ' + req.params.yourName);
});

app.listen(3000, () => console.log('Server running on port 3000'));

Here :yourName stands for a variable string that we can pass in our URL and with req.params.yourName we can read it and send the name back to the user.

If we now call http://localhost:3000/test, we get the string with the given name as response. So you can already read and send data with really little code.

8. FAQ

When do I use Node.js?

Node.js is primarily used for “normal” dynamic websites and backend APIs (RestAPIs). In addition, Node.js is often used in applications that need to process data in real time (e.g. chats).

Is Node.js a programming language?

Definitely no. Node.js connects the scripting language JavaScript and a network component. Since Node.js was developed on the Google V8 JavaScript engine, Node.js masters the basics of network technology such as the HTTP, DNS and TCP protocols.

Is it easy to learn Node.js?

Since Node.js is not a framework and not a programming language, you have to ask yourself if it is easy to learn JavaScript. Surveys have shown that JavaScript is one of the easiest scripting languages to learn.

What is middleware?

A middleware is a part-program, i.e. a function that is executed between two components. For example, if the user calls /settings, the actual program code should only be executed if the user is logged on. To do this, you write a middleware function and call this function before. This is done so that the same code (checking the login) can be used several times, but only has to be written once.

How and where can I host a Node.js app?

For example, you can host Node.js Apps for free at Heroku.

What does the package.json do?

The package.json at Node.js Apps contains information about the name, the author, and much more. Most important are the dependencies. This is a list of the modules (dependencies) your app uses. You can also use scripts to define commands to run or test your app.

What does the node_modules folder do?

The node_modules folder contains all modules included in your node.js. This folder contains standard modules, but also all modules that you have additionally installed and that are listed in the package.json under scripts. You don’t need to push the node_modules folder into your git repository or live hosting, because the npm install command will download all modules into the folder again.

Conclusion

Pooh! Quite a lot of information at once, isn’t it? Nevertheless I hope that you got a good impression of what is possible with Node.js and I made you curious for more. If so, you are welcome to continue looking around on webdeasy.de and read even more exciting articles about web development. You are welcome to write any unsettled questions in the comments! :)

#node #nodejs #node.js #web-development

The Ultimate Node.js Beginner Tutorial
1 Likes12.80 GEEK