Node.js Tutorial

Node.js is a JavaScript runtime built on Chrome's V8 engine that lets developers run JavaScript on the server, enabling full stack development with a single language.

Core Concepts

A Node.js tutorial typically covers the event loop, non-blocking I/O, and the built-in modules that make Node efficient for handling concurrent requests.

Working with Modules

Node.js uses a module system that lets developers organize code into reusable files, importing built-in, third-party, and custom modules as needed.

Building Your First App

Most tutorials guide learners through creating a simple server, handling requests, and responding with data before moving on to frameworks like Express.

Getting Started: Install and Run Your First Script

Download the current LTS (long-term support) release from the official Node.js website, then confirm the install in a terminal:

node -v
npm -v

Create a file named hello.js containing console.log("Hello from Node"); and run it with node hello.js. To start a real project, make a folder and run npm init -y, which creates a package.json file to track your project's name, scripts and dependencies.

A Small HTTP Server Without Any Framework

Node ships with an http module, so you can build a working server using only built-in tools:

// server.js
const http = require("http");

const server = http.createServer((req, res) => {
  if (req.url === "/" && req.method === "GET") {
    res.writeHead(200, { "Content-Type": "text/plain" });
    res.end("Home page");
  } else if (req.url === "/api/time") {
    res.writeHead(200, { "Content-Type": "application/json" });
    res.end(JSON.stringify({ now: new Date().toISOString() }));
  } else {
    res.writeHead(404);
    res.end("Not found");
  }
});

server.listen(3000, () => console.log("Listening on port 3000"));

Run node server.js and open http://localhost:3000/api/time in a browser. Writing routes by hand like this shows why frameworks exist: Express handles routing, parsing and middleware so you do not repeat this logic everywhere.

The Event Loop in Plain Words

Node runs your JavaScript on a single main thread. When code asks for something slow, such as reading a file or querying a database, Node hands the job to the system and moves on. When the job finishes, its callback is placed in a queue, and the event loop runs it as soon as the main thread is free.

const fs = require("fs/promises");

console.log("1. start");

fs.readFile("notes.txt", "utf8")
  .then(text => console.log("3. file length:", text.length));

console.log("2. end of script");

The output prints 1, then 2, then 3, because reading the file does not block the rest of the script. Modern code usually writes the same thing with async and await, which reads top to bottom but still does not block.

Modules and Beginner Pitfalls

CommonJS vs ES modules

Node supports two module systems. CommonJS uses require() and module.exports. ES modules use import and export, the same syntax as browser JavaScript. To use ES modules, add "type": "module" to package.json or name files with the .mjs extension. Pick one style per project to keep things simple.

Beginner mistakes in Node.js

  • Blocking the event loop with heavy loops or synchronous calls like readFileSync inside a request handler, which freezes every other user.
  • Unhandled promise rejections. Always catch errors or wrap await in try/catch.
  • Committing node_modules instead of listing it in .gitignore.
  • Hard-coding secrets. Read keys and passwords from environment variables via process.env.

Suggested learning path: core modules, npm, async patterns, then Express, a database such as MongoDB, and finally connecting a React front end.

Ready to Master Node.js?

Join Uncodemy's industry-recognized Node.js Training Course and build real, job-ready skills.

Explore the Course