What is Node.js?

You might know that JavaScript can write code in browsers to make web pages interactive. But Node.js takes JavaScript “out of the browser” and lets it run on the server side. It’s like a “JavaScript backend butler” that lets you use familiar JavaScript syntax to develop backend programs, such as building website servers or handling database requests.

Node.js is built on Google’s V8 engine (the same engine that runs JavaScript in browsers), extending JavaScript from the frontend to the backend and enabling “full-stack JavaScript development.”

Why Learn Node.js?

For beginners, Node.js has several special advantages:
- Full-Stack JavaScript Development: Use JavaScript for both frontend and backend, avoiding the need to learn multiple languages (e.g., frontend HTML/CSS/JS, backend Python/Java). This improves code reusability.
- Non-Blocking I/O: Imagine ordering takeout—traditional backends might “wait for one order to finish before taking the next.” Node.js is like a “waiter who takes 10 orders at once,” efficiently handling massive concurrent requests (e.g., chat apps, live streaming).
- Lightweight and Efficient: Ideal for small, fast projects like personal blogs, API interfaces, or real-time chat tools.
- Rich Ecosystem: Through npm (Node Package Manager, similar to frontend package managers), you can easily install various toolkits for database handling, file operations, route management, etc.

Installing Node.js (Simple Steps)

  1. Download from the Official Website: Visit the Node.js official site, and we recommend the “LTS version” (Stable version, suitable for beginners).
  2. Installation: For Windows/Mac/Linux, simply double-click the installer and follow the prompts. (Windows users: Ensure “Add to PATH” is checked to enable command-line access.)
  3. Verify Installation: Open your command-line tool (cmd/PowerShell for Windows, Terminal for Mac/Linux) and enter:
   node -v   # Shows Node.js version, e.g., v20.10.0
   npm -v    # Shows npm version, e.g., 10.2.3

If version numbers appear, the installation is successful!

First Node.js Program: “Hello World” Server

Let’s create a simple HTTP server with Node.js that returns “Hello World” when accessed.

  1. Create a File: In any folder, create a new text file and rename it server.js (the suffix must be .js).
  2. Write the Code:
   // Import Node.js's built-in http module (tool for handling HTTP requests)
   const http = require('http');

   // Create a server instance to handle requests and responses
   const server = http.createServer((req, res) => {
     // Set response headers: Status code 200 (success), content type text
     res.writeHead(200, { 'Content-Type': 'text/plain' });
     // Response content: Hello World
     res.end('Hello World!\n');
   });

   // Make the server listen on port 3000 (port number can be changed, e.g., 8080)
   const PORT = 3000;
   server.listen(PORT, () => {
     console.log(`Server started! Visit http://localhost:${PORT}`);
   });
  1. Run the Program: In the command line, navigate to the folder containing server.js and enter:
   node server.js

The command line will display Server started! Visit http://localhost:3000.
4. Test: Open a browser and go to http://localhost:3000—you’ll see “Hello World!”!

Node.js Core Capabilities: File Operations & Package Management

Beyond HTTP servers, Node.js can do much more:

1. File System (fs Module)

The fs module lets you read/write local files. For example, read a text file:

const fs = require('fs');

// Asynchronous file reading (non-blocking, doesn't block other operations)
fs.readFile('example.txt', 'utf8', (err, data) => {
  if (err) {
    console.error('Read failed:', err);
    return;
  }
  console.log('File content:', data);
});

Note: Create an example.txt file first (with content in the same folder), then run node filename.js to see the content.

2. Package Manager (npm)

npm is Node.js’s “App Store,” where you can install pre-built toolkits. For example, install the figlet package for ASCII art:
- Install the Package: Run in the command line:

  npm install figlet  # --save saves dependencies to package.json
  • Use the Package:
  const figlet = require('figlet');
  figlet('Node.js is fun!', (err, data) => {
    console.log(data);
  });

Running this will display ASCII art for “Node.js is fun!”.

Summary: Node.js is Easy to Learn—Practice Makes Perfect

Node.js has a low learning curve and intuitive code, making it perfect for beginners to get started quickly. Start with a “Hello World” server, then experiment with file operations and third-party packages to experience the joy of developing backends with JavaScript.

Next, explore frameworks like Express (simplifies routing, middleware, etc.) or build full-stack projects (e.g., a database-backed blog). Remember: the key to programming is hands-on practice—don’t fear mistakes; consistent coding will lead to progress!

Now, try writing your own “small project”—for example, build a simple to-do list API with Node.js, or read a local image and return it to the browser!

Xiaoye