Node.js Tutorial

Welcome to Node.js Tutorial – We shall learn Node.js basics and advanced concepts to program and build server side applications and networking applications.

Node.js is a JavaScript runtime used to run JavaScript outside the browser. It is commonly used for web servers, APIs, command-line tools, file processing, real-time applications, and backend services. This tutorial page gives you a structured path to learn Node.js from setup and modules to HTTP servers, file system operations, JSON handling, MySQL integration, and practical examples.

Node.js Tutorial

Node.js Tutorial Index for Beginners and Backend Practice

Following is the list of tutorials that are well explained with simplified examples to learn Node.js easily.

What Node.js Is and Where It Fits in Web Development

Node.js is a JavaScript runtime built on Chrome’s V8 JavaScript engine. It lets you use JavaScript for server-side programming, not only for browser scripts. A Node.js program can read files, create an HTTP server, connect to a database, call another API, or run a background task.

The two features that make Node.js stand out are:

  1. Event-driven execution
  2. Non-blocking I/O model

In practical terms, Node.js is useful when an application spends time waiting for I/O operations such as database queries, file reads, network requests, or API calls. Instead of blocking the entire program while waiting, Node.js can continue handling other work and then respond when the operation completes.

Official references you may use while learning are the Node.js introduction, the Node.js Learn documentation, and the MDN introduction to Express and Node.js.

How Node.js Handles Requests with Events and Non-Blocking I/O

To understand Node.js event handling, compare it with a simple blocking flow. In a blocking server, one request may wait while the program reads a file, talks to a database, or calls another service. During that waiting time, the same thread cannot continue with other work.

Node.js follows an event-driven model. A request can start an I/O operation and register what should happen when the operation finishes. While the I/O work is pending, Node.js can continue processing other events. When the file read, database query, or network call completes, the callback, promise, or async function continuation handles the result.

This does not mean every Node.js program is automatically faster. CPU-heavy work can still block the event loop. Node.js works best when the application has many I/O operations and the code is written to avoid long-running synchronous tasks in the request path.

Set Up Node.js and Check the Runtime from Terminal

Before writing a Node.js program, install Node.js and check that the node and npm commands are available from your terminal or command prompt.

</>
Copy
node -v
npm -v

The output shows the installed Node.js and npm versions.

v20.11.1
10.2.4

Your version numbers may be different. For learning the examples in this tutorial series, the important part is that both commands run successfully.

Create Your First Node.js Script

Create a file named hello-node.js and add the following code.

</>
Copy
console.log("Hello from Node.js");

Run the file using the node command.

</>
Copy
node hello-node.js

The program prints the message in the terminal.

Hello from Node.js

Create a Simple Node.js HTTP Server

One of the first backend examples in Node.js is a small HTTP server. The following program uses the built-in http module and sends a plain response to the browser.

</>
Copy
const http = require("http");

const server = http.createServer(function (req, res) {
    res.writeHead(200, { "Content-Type": "text/plain" });
    res.end("Hello from a Node.js server");
});

server.listen(3000, function () {
    console.log("Server is running at http://localhost:3000/");
});

Save the file as server.js and run it from the terminal.

</>
Copy
node server.js

Open http://localhost:3000/ in the browser. The browser receives the response generated by the Node.js program.

Programming Node.js Applications with JavaScript Modules

Node.js applications are written in JavaScript. This allows the same language to be used for browser code and server-side code, although the available APIs are different in each environment. For example, browser JavaScript can work with the DOM, while Node.js can work with files, network sockets, operating system paths, and server processes.

In day-to-day Node.js development, you will use modules to divide a program into smaller files. A module may export a function, object, class, or value so that another file can import and use it. Node.js supports built-in modules such as fs, http, path, and events, and it also supports packages installed through npm.

</>
Copy
// math-utils.js
function add(a, b) {
    return a + b;
}

module.exports = { add };
</>
Copy
// app.js
const mathUtils = require("./math-utils");

console.log(mathUtils.add(10, 20));

This example uses CommonJS syntax with require() and module.exports, which is still common in many Node.js examples. You may also see ES module syntax with import and export in modern projects.

Node.js package.json and npm Packages

A real Node.js project usually has a package.json file. It stores project metadata, scripts, and dependency information. You can create one with npm init or npm init -y.

</>
Copy
npm init -y

A small package.json file may look like this.

</>
Copy
{
  "name": "nodejs-learning-app",
  "version": "1.0.0",
  "main": "server.js",
  "scripts": {
    "start": "node server.js"
  }
}

After adding a start script, you can run the project using npm.

</>
Copy
npm start

Node.js Learning Path After the Basics

Once you can run JavaScript with Node.js and create a small server, continue with these areas in order. This sequence helps you connect the tutorials in this section with real backend programming tasks.

  • Core modules: Learn fs, path, http, events, and other built-in modules.
  • npm and package.json: Install packages, manage scripts, and understand project dependencies.
  • HTTP servers and routing: Create routes, return responses, and handle request methods.
  • File system operations: Read, write, create, delete, and copy files using Node.js.
  • JSON data: Parse JSON, create JSON objects, and write JSON data to files.
  • Database access: Connect Node.js with MySQL and run basic SQL queries from JavaScript.
  • Express basics: After the built-in HTTP module, learn Express for routing, middleware, and API development.

When Node.js Is a Good Choice for a Project

Node.js is a good fit for many backend tasks, especially when the application handles many small network or file operations. It is often used for REST APIs, web dashboards, JSON services, real-time features, automation scripts, and command-line tools.

Node.js may not be the best first choice for a request that performs long CPU-heavy calculations in the main event loop. Such work can delay other requests unless it is moved to workers, queues, native services, or a separate processing layer.

Common Mistakes While Learning Node.js

  • Using browser-only APIs in Node.js: Node.js does not provide the browser DOM, so objects such as document and window are not available in normal Node.js scripts.
  • Forgetting asynchronous behavior: File, database, and network operations may finish later, so write code that handles callbacks, promises, or async/await correctly.
  • Blocking the event loop: Avoid long synchronous work inside request handlers.
  • Ignoring package.json: Keep scripts and dependencies in package.json so the project can be installed and run consistently.
  • Skipping error handling: Always handle errors from file operations, network calls, and database queries.

Node.js Basics FAQ

Is Node.js a programming language?

No. Node.js is not a separate programming language. It is a runtime environment that lets you run JavaScript outside the browser, commonly on a server or local machine.

Do I need to know JavaScript before learning Node.js?

Yes, basic JavaScript knowledge is helpful before learning Node.js. You should be comfortable with variables, functions, objects, arrays, callbacks, promises, and basic error handling.

What is npm in Node.js?

npm is the package manager commonly installed with Node.js. It is used to install packages, run project scripts, and manage dependencies listed in package.json.

Can Node.js be used without Express?

Yes. Node.js includes a built-in http module that can create web servers. Express is a popular framework that simplifies routing, middleware, and request handling, but it is not required for basic Node.js programs.

What should I learn after Node.js basics?

After the basics, learn npm, modules, asynchronous programming, file system operations, HTTP servers, Express, database access, environment variables, and API error handling.

QA Checklist for This Node.js Tutorial Page

  • Confirm that the page clearly explains Node.js as a JavaScript runtime, not a separate language.
  • Confirm that beginner topics are ordered from installation and first script to modules, HTTP, file system, JSON, and database examples.
  • Confirm that command-line examples use language-bash and terminal results use the output class.
  • Confirm that JavaScript and JSON code blocks use PrismJS-compatible language classes.
  • Confirm that the tutorial links to practical next steps such as installation, HTTP server, file system, MySQL, JSON, and examples.

Next Steps in This Node.js Tutorial Series

Start by installing Node.js, then run a simple script, create a small HTTP server, and continue with modules and file system operations. After that, move to JSON handling, npm packages, database examples, and Express-based backend development.