Chapter 2: Intro to Node.js & Express
Welcome to Chapter 2! In the last chapter, we decided to learn **Node.js** because we already know JavaScript. But what is Node.js exactly? And what is this "Express" framework everyone talks about?
What is Node.js?
Node.js is **not** a language. It's a **runtime environment**. This means it's a program that lets you run your JavaScript code *outside* of a web browser. Node.js lets you use JavaScript to do "server-side" tasks, like creating web servers, connecting to databases, and reading files.
What is Express.js?
Node.js *can* create a web server all by itself, but the code is very complicated. **Express.js** is a **framework** that sits on top of Node.js and makes everything *much* easier. It gives you a clean, simple set of tools to handle common web server tasks.
Node.js is the car engine. It's powerful, but you can't drive it on its own.
Express.js is the rest of the car. It's the chassis, steering wheel, and pedals that connect to the engine and let you "drive" your server.
Let's Build Your First Server (Step-by-Step)
To follow along, you need to have Node.js installed. Open your terminal and follow these steps.
Step 1: Set Up Your Project
Create a new folder, move into it, and initialize it with **npm** (the Node Package Manager).
# Create a new folder and move into it
mkdir my-first-server
cd my-first-server
# Initialize the project
npm init -y
This command creates a `package.json` file, which keeps track of all the packages your project needs.
Step 2: Install Express
# Install Express
npm install express
Step 3: Create Your Server File
Create a new file in your folder named `server.js`.
Step 4: Write the "Hello World" Server Code
Open `server.js` and type in the following code.
// 1. Import the Express library
const express = require('express');
// 2. Create an instance of an Express app
const app = express();
// 3. Define a port for our server to run on
const PORT = 3000;
// 4. Define the main route ('/')
// req = request (what the user sends)
// res = response (what we send back)
app.get('/', (req, res) => {
res.send('Hello, World! This is my first server!');
});
// 5. Start the server
app.listen(PORT, () => {
console.log(`Server is running live on http://localhost:${PORT}`);
});
Step 5: Run Your Server!
Save the file. Go back to your terminal and run this command:
node server.js
Your terminal should show: `Server is running live on http://localhost:3000`. Now, open your web browser and go to **http://localhost:3000** to see your message!
Core Concept: Routing
**Routing** is how Express handles different URLs. You just add more `app.get()` handlers for different paths.
// ... (setup code is the same) ...
// Route for the homepage
app.get('/', (req, res) => {
res.send('This is the HOMEPAGE.');
});
// NEW: Route for the about page
app.get('/about', (req, res) => {
res.send('This is the ABOUT page.');
});
// ... (app.listen code is the same) ...
Core Concept: Sending JSON (Building an API)
Most of the time, your backend will send **JSON** (JavaScript Object Notation) data to your frontend (like a React app). Express has a built-in method, `.json()`, to do this.
// ... (setup code is the same) ...
// A route for sending user data
app.get('/api/user', (req, res) => {
const user = {
id: 1,
username: 'MSMAXPRO'
};
// Send the object as JSON
res.json(user);
});
// ... (app.listen code is the same) ...
Now, if you go to `http://localhost:3000/api/user`, you'll see structured JSON data, ready for your React app to fetch!
Conclusion and Next Steps
You've just learned the fundamentals of Node.js and Express. You know how to start a project, create a server, handle routes, and send API data. The next step is to learn how to connect your Express server to a **Database (like MongoDB)** to save data permanently.