Writing Your First Web Server with Node.js: A Quick Start with the Express Framework

This article introduces the method of building a web server using Node.js and Express. Based on the V8 engine, Node.js enables JavaScript to run on the server side, while Express, as a popular framework, simplifies complex tasks such as routing and request handling. For environment preparation, first install Node.js (including npm), and verify it using `node -v` and `npm -v`. Next, create a project folder, initialize it with `npm init -y`, and install the framework with `npm install express`. The core step is writing `server.js`: import Express, create an instance, define a port (e.g., 3000), use `app.get('/')` to define a GET request for the root path and return text, then start the server with `app.listen`. Access `http://localhost:3000` to test it. Extended features include adding more routes (e.g., `/about`), dynamic path parameters, returning JSON (`res.json()`), and hosting static files (`express.static`). The key steps are summarized as: installing tools, creating a project, writing routes, and starting the test, laying the foundation for subsequent learning of middleware, dynamic routing, etc.

Read More
Beginner's Guide to Nginx: From Installation to Reverse Proxy Configuration

Nginx is a high-performance HTTP and reverse proxy server, lightweight and stable, suitable for scenarios such as website building and load balancing. Installation is divided into Ubuntu/Debian (`sudo apt install nginx`) and CentOS/RHEL (`sudo yum install nginx`). Verification is done with `nginx -v`. Start the service (`sudo systemctl start nginx`) and set it to start automatically (`sudo systemctl enable nginx`). Management commands include start/stop, restart, and reload configuration (`reload`). For core reverse proxy configuration: Create a site configuration file (e.g., `myapp.conf`) in `/etc/nginx/conf.d/`. Example configuration: The `server` listens on port 80, `server_name` is set to the domain name/IP, `location /` forwards to the backend port (e.g., `127.0.0.1:3000`) via `proxy_pass`, and passes Host and real IP through `proxy_set_header`. After configuration, check syntax with `nginx -t`, and `reload` to apply changes, then test access to the backend content. Notes: Open ports 80/443 in the firewall, ensure the backend service is running, and `proxy_pass` must start with `http://`/`https://`.

Read More