Node.js Essentials: A Step-by-Step Guide

Noran Saber Abdelfattah
4 min readApr 5, 2024

Introduction

Nodejs

Our tutorial series on Node.js offers comprehensive guidance for both beginners and experienced developers, covering essential concepts from event-driven programming to building robust web applications and APIs. Each article provides practical examples and hands-on exercises to reinforce learning. By the end of the series, you’ll have the skills and confidence to leverage Node.js effectively in creating dynamic, high-performance applications. Join us as we explore and unlock the potential of Node.js together!

What is Node.js

in w3school, node js is

Node.js is an open source server environment.

Node.js allows you to run JavaScript on the server.

Sure, let’s break down and explain what that means.

Node.js

  • Imagine you have a computer that can talk to other computers on the internet. Node.js is like a special tool that helps your computer understand and process requests from other computers. It’s like a language your computer speaks to handle stuff on the internet.

Open source:

  • Imagine you have a toy that you can take apart and see how it works. Open source means the “blueprints” of Node.js are freely available for anyone to see and change. It’s like having the instructions to make your toy better or fix any problems.

For exmaple

  • Imagine you have a recipe for making your favourite dish, let’s say chocolate chip cookies. Now, if you decide to share that recipe with your friends so they can make the cookies too, that’s a bit like open source.
  • Open source means that the “recipe” (or code) used to make software or tools is freely available for anyone to see, use, and even modify. It’s like saying, “Hey, here’s how I made this thing, feel free to take a look, use it, and even make changes to it if you want!”
  • So, when something is open source, it’s like sharing your recipe for chocolate chip cookies with the world, allowing others to bake their own versions, tweak the ingredients, and maybe even improve upon the original recipe.

Server environment:

  • Imagine you have a big box that can store and serve snacks to your friends. In the world of computers, a server is like that big box, that stores and sends out information to other computers. Node.js helps make your computer act like this big box, handling requests and serving information to other computers.

Why to learn Node.js

  1. One Language, Two Jobs: With Node.js, you use the same language (JavaScript) to build both the parts of a website that people see and the parts that work behind the scenes. It’s like using the same crayon to draw the picture and write the story!
  2. Speedy Tasks: Node.js helps your computer do many things quickly by managing tasks in a smart way. It’s like having a super-fast helper who can juggle multiple tasks without slowing down.
  3. Lots of Uses: You can use Node.js to build all sorts of things, like websites, apps where you can talk in real time, or tools that help different programs communicate with each other. It’s like having a toolbox full of gadgets that can do lots of different jobs!

How to install

On Windows

Installing Node.js is a simple process. Windows users can find an installer package on the official downloads page of Node.js. Just download the package and run the installation wizard to complete the installation.

On Ubuntu OS

sudo apt update
sudo apt install nodejs

What is NPM?

NPM, short for Node Package Manager, is a tool used through the command line.

It helps with installing Node.js packages, managing their versions, and handling dependencies between them.

NPM gives you access to a vast library of code made by other people, which you can easily use in your own projects. It’s like having a friend who shares their cool toys with you to help you build your project faster and easier. Instead of starting from scratch, you can leverage the work of others to make your own creations better!

To install any module/Package

npm install <Module Name>

How to create a Node.js Application?

  • Make a file called hello.js
// hello.js
var http = require('http');

http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World!');
}).listen(8080);

Let’s break it down

var http = require('http');
  • you’re telling Node.js to include the built-in http module so that you can use its functionality in your program.
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World!');
}).listen(8080);
  1. Using createServer(): you're using the createServer() method from the HTTP module to create an HTTP server.
  2. Handling Request and Response: The function (req, res) => {...} passed to createServer() represents the request and response handling. The req parameter represents the request coming from the client (like a web browser) and res represents the response that your server will send back to the client.
  3. Setting Response Headers: With res.writeHead(200, {'Content-Type': 'text/plain'}), you're setting the response header. The status code 200 indicates that the request was successful ("OK"), and 'Content-Type': 'text/plain' indicates that the response will contain plain text.
  4. Sending Response: res.end('Hello World!') sends the response back to the client. In this case, it sends the text "Hello World!" as the response body
  5. Listening on Port: with .listen(8080), you're instructing the server to listen for incoming requests on port 8080.

Don’t forget that

Your server, created using Node.js with the http.createServer() method, is indeed the server-side part. It's responsible for receiving requests from clients (like web browsers) and sending responses back to them.

The web browser, or any other client making requests to your server, is the client-side part. It sends requests to your server (for example, when a user enters a URL in the browser's address bar or clicks a link) and receives responses from the server (like the "Hello World!" message in this case).

Your feedback is invaluable! ❤️

Feel free to connect 🌹me on:

Twitter, LinkedIn, GitHub

Noran🌹

--

--