Building Your First Multi-Page Website with a Node.js Backend

This guide will walk you through creating a simple, yet fully functional, multi-page website from scratch. You will build the frontend with HTML, CSS, and JavaScript, and power the contact form with a lightweight Node.js and Express backend.

What You'll Learn:

Part 1: Project Overview & File Structure

We will build a three-page website: a Home page, an About page, and a Contact page. The contact form will submit data to our Node.js server, which will save the messages into a JSON file.

Final Folder Structure

Visualizing the structure helps understand how the pieces connect. A clean separation between frontend (`public`) and backend (`server`) code is a professional standard that makes the project easier to manage.

my-website/
β”œβ”€β”€ public/                 # Contains all frontend files served to the browser
β”‚   β”œβ”€β”€ index.html          # Home page
β”‚   β”œβ”€β”€ about.html          # About page
β”‚   β”œβ”€β”€ contact.html        # Contact page
β”‚   β”œβ”€β”€ css/
β”‚   β”‚   └── styles.css      # All our styling rules
β”‚   └── js/
β”‚       └── script.js       # Client-side JavaScript
β”œβ”€β”€ server/                 # Contains our backend server logic
β”‚   β”œβ”€β”€ server.js           # The main Express server file
β”‚   └── messages.json       # A simple file to store form submissions (created automatically)
└── package.json            # Node.js project configuration file

Part 2: Prerequisites

Before you begin, ensure you have the following tools installed. You can check if you have Node.js and npm installed by opening your terminal and running `node -v` and `npm -v`.

Part 3: Setting Up the Project

Let's create the project folder and initialize it as a Node.js project. This setup process creates a `package.json` file, which acts as the "ID card" for our project, tracking its name, version, and the external libraries (dependencies) it needs to run.

Step 1: Create the Project Directory

Open your terminal and run these commands one by one to create a folder and navigate into it.

# Create a new folder named 'my-website'
mkdir my-website

# Navigate into the newly created folder
cd my-website

Step 2: Initialize the Node.js Project

Now, initialize a Node.js project using npm. This creates a `package.json` file to track our project's details and dependencies.

# The '-y' flag accepts all the default settings
npm init -y

Step 3: Install Required Packages

We need a few helper libraries (packages) for our server. Let's install them.

npm install express body-parser cors

Part 4: Building the Frontend (HTML, CSS, & JS)

This is the part of your website that users will see and interact with in their browser. We will keep it clean and simple.

Step 1: Create the public Folder and Subfolders

This command creates the `public` directory along with its `css` and `js` subdirectories in one go.

mkdir -p public/css public/js

Step 2: Create the HTML Pages

Create the three HTML files inside the `public/` folder. Notice the consistent structure with `

`, `
`, and `